有了这些,java IO就不愁了
IO的总结:
- java中相对路径和绝对路径的问题:
- 在web项目中,如果生成的文件前面没有
/
开头的话,表示的是生成的文件在当前项目的根目录下如student.txt
在项目中刷新就能看到。 - 如果是以
/
开头的话,如/student.txt
则表示文件存放在磁盘根目录下(D://student.txt)。(好像也有可能在eclipse的根目录下) - 也可以将文件写在绝对路径下,如
C://user/student.txt
则表示文件在C盘的user目录下。
- 在web项目中,如果生成的文件前面没有
1. 基本流: InputStream、OutputStream、Reader、Writer等基本的输入输出,在使用的时候通过这些抽象方法的实现类(FileInputStream、FileOutputStream、FileReader、FileWriter)等来进行读取的读取和写入主要是通过 int b = fis.read()
或fos.write(b)
这种方式来读取或者写入的。
2. BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter等带有Buffer关键字的,通常情况下是在1的情况下再套一层Buffer流,原理就是先将数据写入到缓存中,再从缓存中将数据取出来。
- Buffer读取流的时候,使用的是
bis.Reader()
来读取的而BufferedReader则是使用br.readLine()
来读取一行。 - 当使用BufferedWriter来写入数据的时候有一个
bw.flush()
方法,主要目的是将缓存中的数据强制写出去。
3. Data流: 主要是可以对基本数据类型进行写入写出等操作
- Data流写数据:
创建一个字节数组输出流 ByteArrayOutputStream。
创建 DataOutputStream 对象来对ByteArrayOutputStream进行数据的写入操作。
可通过 dos.writeBoolean() writeString() writeDouble() ... 。 - Data流读数据:
创建一个字节数组输入流 ByteArrayInputStream();
创建 DataInputStream对象来对上述字节流读取。
可以通过dis.readBoolean() readString() ....
4. Print流: 常见的是System.out.print()来输出
- PrintStream可以指定输出对象,并不一定要输出在控制台,可以输出在文件中等。
创建输出文件(FileOutputStream fos = new FileOutputStream("txt/unicode.txt");)、设置输出位置(System.setOut(ps);)、打印输出对象(System.out.println(i);)即可完成指定输出。 - PrintWriter对象,可以指定将数据输出到文件中。
- 使用PrintStream读取文件数据。
5. ObjectOutputStream/ObjectInputStream 利用这两个对象可以存放序列化的数据。
6. 随机读取File文件:RandomAccessFile提供了seek()方法,用来定位将要读写文件的指针位置,我们也可以通过调用getFilePointer()方法来获取当前指针的位置。
7. 管道:管道主要用来实现同一个虚拟机中的两个线程进行交流。因此,一个管道既可以作为数据源媒介也可作为目标媒介。需要注意的是java中的管道和Unix/Linux中的管道含义并不一样,在Unix/Linux中管道可以作为两个位于不同空间进程通信的媒介,而在java中,管道只能为同一个JVM进程中的不同线程进行通信。和管道相关的IO类为:PipedInputStream和PipedOutputStream。
1.文件输入输出流
/* 使用文件输入输出流的方式将sa图片输出到另一个位置*/
public class Test1FileInputStream {
private static FileInputStream in = null;
private static FileOutputStream out = null;
public static void main(String[] args) {
int b = 0;
try {
in = new FileInputStream("pic/sa.jpg");
out = new FileOutputStream("output/pic/sb.jpg");
while ((b = in.read()) != -1) {
System.out.print((char) b);
out.write(b);
}
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
System.out.println("over");
}
}
}
2.文件读取类
/*从文件中读取数据*/
public class Test3FileReader {
public static void main(String[] args) {
int c = 0;
try {
FileReader reader = new FileReader("a.txt");
while ((c = reader.read()) != -1) {
System.out.print((char) c);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.文件写入类
/* 将数据写入文本中*/
public class Test4FileWriter {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("a.txt");
for (int i = 0; i < 65535; i++) {
writer.write((char) i);
}
writer.close();
System.out.println("success");
} catch (IOException e) {
e.printStackTrace();
System.out.println("err");
System.exit(-1);
}
}
}
4.使用流的方式读取文件
/*读取并输出文件内容*/
public class Test5BufferStream {
public static void main(String[] args) {
try {
FileInputStream inputStream = new FileInputStream("mark.log");
BufferedInputStream bis = new BufferedInputStream(inputStream);
// System.out.println(bis.read());
// System.out.println(bis.read());
int c = 0;
while ((c = bis.read()) != -1) {
System.out.print((char) c);
}
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
5.使用缓存的方式读取和写入文件
/*向一个文件中写入缓存数据并读取出来*/
public class Test6BufferStream1 {
public static void main(String[] args) {
try {
// 通过缓存区向文件中写入数据
BufferedWriter bw = new BufferedWriter(new FileWriter("txt/a.txt"));
String s = null;
for (int i = 0; i < 100; i++) {
s = String.valueOf(Math.random());
bw.write(s);
bw.newLine();
}
bw.flush();
// 从文件中将数据读取到缓冲区在读取出来
BufferedReader br = new BufferedReader(new FileReader("txt/a.txt"));
while ((s = br.readLine()) != null) {
System.out.println(s);
}
bw.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
6.将输入的内容打印出来
/*将数据从console中读出*/
public class Test7TransForm {
public static void main(String[] args) {
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader reader2 = new BufferedReader(reader);
String s = null;
try {
s = reader2.readLine();
while (s != null) {
if (s.equalsIgnoreCase("exit")) {
System.exit(0);
}
System.out.println(s);
s = reader2.readLine();
}
reader2.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
7.使用OutputStreamWriter类将数据写入到文件中
/*OutputStreamWriter类*/
public class Test8TransForm1 {
public static void main(String[] args) {
try {
// 如果为true表示在原来的基础上追加 数据而不是覆盖
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("txt/b.txt", true), "utf-8");
osw.write("abcdefghijklmnopqrst ");
osw.flush();// 将缓存中的数据强制发送出去
System.out.println(osw.getEncoding());
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
8.ByteArrayOutputStream类
/*利用dataStream可以向文件中写入各种类型的数据*/
public class Test9DataStream {
public static void main(String[] args) throws IOException {
// 创建字节数组
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 创建数据输出流
DataOutputStream dos = new DataOutputStream(baos);
dos.writeDouble(Math.random());
dos.writeBoolean(true);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
System.out.println(bais.available());// 八个字节
DataInputStream dis = new DataInputStream(bais);
System.out.println(dis.readDouble());
System.out.println(dis.readBoolean());
dos.close();
dis.close();
}
}
9.使用PrintStream将数据输出到文件中
/*将输出输出到文件中*/
public class TestPrintStream1 {
private static PrintStream ps = null;
public static void main(String[] args) throws Exception {
// 创建输出的文件
FileOutputStream fos = new FileOutputStream("txt/unicode.txt");
// 创建输出流
ps = new PrintStream(fos);
if (ps != null) {
// 指定输出的位置
System.setOut(ps);
}
int in = 0;
// 将数据输出
for (char i = 0; i < 60000; i++) {
System.out.print(i + " ");
in++;
if (in >= 50) {
System.out.println();
in = 0;
}
}
System.out.println("over");
}
}
10.日志的基本原理
/*将输入的内容记录到日志中*/
public class TestPrintStream2 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = null;
FileWriter writer = new FileWriter("mark.log", true);
PrintWriter log = new PrintWriter(writer);
while ((s = br.readLine()) != null) {
if (s.equalsIgnoreCase("exit")) {
break;
}
System.out.println(s);
log.println("--------------");
log.println(s);
log.flush();
}
log.print("==" + new Date() + "==");
log.flush();
log.close();
}
}
11.使用缓存区文件读取
/*使用输出流读取文件内容,显示在console中*/
public class TestPrintStream3 {
public static void main(String[] args) {
String fileName = "mark.log";
if (fileName != null) {
list(fileName, System.out);
}
}
private static void list(String file, PrintStream ps) {
try {
// 创建缓冲区读取对象
BufferedReader br = new BufferedReader(new FileReader(file));
String s = null;
// 循环读取缓冲区的内容
while ((s = br.readLine()) != null) {
// 将数据读取到控制台上
ps.println(s);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
ps.print("读取错误");
}
}
}
12.对象输入输出流
/* transient 修饰的成员变量在序列化的时候不予考虑*/
public class TestObjectIO {
public static void main(String[] args) {
T t = new T();
t.i = 6;
try {
// 将对象写到文本中
FileOutputStream fos = new FileOutputStream("txt/object.io");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(t);
oos.flush();
oos.close();
// 将对象从文本中读出来
FileInputStream fis = new FileInputStream("txt/object.io");
ObjectInputStream ois = new ObjectInputStream(fis);
T t1 = (T) ois.readObject();
System.out.println(t1.d + " " + t1.i + " " + t1.j + " " + t1.k);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class T implements Serializable {
double d = 2.3;
int i = 10;
int j = 9;
transient int k = 2;
}
13. 随机读写
/*随机读写文件*/
public class TestAccessFile {
public static void randomAccessFileWrite() throws IOException {
// 创建一个RandomAccessFile对象
RandomAccessFile file = new RandomAccessFile("d:/test.txt", "rw");
// 通过seek方法来移动读写位置的指针
file.seek(10);
// 获取当前指针
long pointerBegin = file.getFilePointer();
// 从当前指针位置开始写
file.write("hehe 你好牛鞭".getBytes());
long pointerEnd = file.getFilePointer();
System.out.println("pointerBegin:" + pointerBegin + "\n" + "pointerEnd:" + pointerEnd + "\n");
file.close();
}
public static void randomAccessFileRead() throws IOException {
// 创建一个RandomAccessFile对象
RandomAccessFile file = new RandomAccessFile("d:/test.txt", "rw");
// 通过seek方法来移动读写位置的指针
file.seek(10);
// 获取当前指针
long pointerBegin = file.getFilePointer();
// 从当前指针开始读
byte[] contents = new byte[1024];
file.read(contents);
long pointerEnd = file.getFilePointer();
System.out.println(
"pointerBegin:" + pointerBegin + "\n" + "pointerEnd:" + pointerEnd + "\n" + new String(contents));
file.close();
}
public static void main(String[] args) throws Exception {
randomAccessFileWrite();
randomAccessFileRead();
}
}
14. 管道Demo
public class TestPipe {
public static void main(String[] args) throws IOException {
// 实现在不同线程中对同一对象进行读写操作
final PipedOutputStream pos = new PipedOutputStream();
final PipedInputStream pis = new PipedInputStream(pos);
// 在第一个线程中对管道进行写入操作
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
pos.write("Hello world, pipe!".getBytes());
} catch (IOException e) {
}
}
});
// 在第二个线程中对管道数据进行读取操作
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
int data = pis.read();
while (data != -1) {
System.out.print((char) data);
data = pis.read();
}
} catch (IOException e) {
} finally {
try {
pis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
t1.start();
t2.start();
}
}
有了这些,java IO就不愁了的更多相关文章
- Java io流的概述
Java语言定义了许多专门负责各种方式的输入/输出,这些类都被放在java.io包中.其中,所有输入流类都是抽象类InputStream(字节输入流)或抽象类Reader(字符输入流)的子类:而所有输 ...
- java.io.IOException: The same input jar is specified twice
简介: eclipse android proguard 打包时出现 java.io.IOException: The same input jar is specified twice 错误, 这里 ...
- 解决: java.io.IOException: 打开的文件过多 的问题
问题 前一阵子公司项目做了一次压力测试, 中间出现了一个问题: 在50多个并发的时候会出现 java.io.IOException: 打开的文件过多 这个异常. 但是在没有并发的时候是不会出现这个问题 ...
- java.IO输入输出流:过滤流:buffer流和data流
java.io使用了适配器模式装饰模式等设计模式来解决字符流的套接和输入输出问题. 字节流只能一次处理一个字节,为了更方便的操作数据,便加入了套接流. 问题引入:缓冲流为什么比普通的文件字节流效率高? ...
- Java:IO流与文件基础
Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...
- Java IO之字符流和文件
前面的博文介绍了字节流,那字符流又是什么流?从字面意思上看,字节流是面向字节的流,字符流是针对unicode编码的字符流,字符的单位一般比字节大,字节可以处理任何数据类型,通常在处理文本文件内容时,字 ...
- java Io流向指定文件输入内容
package com.hp.io; import java.io.*; public class BufferedWriterTest{ public static void main(String ...
- java Io文件输入输出流 复制文件
package com.hp.io; import java.io.FileInputStream; import java.io.FileNotFoundException; import java ...
- java Io流更新文件内容
package com.hp.io; import java.io.FileOutputStream; import java.io.IOException; public class FileOut ...
随机推荐
- Jquery简单使用
展示:$("#id").show(); 隐藏:$("#id").hide();
- CSS rem长度单位
1. 概述 1.1 说明 rem是css3中新增的一个单位属性(font size of the root element),根据页面的根节点(html)的字体大小进行转换的单位,通过此单位属性可以进 ...
- C#生成Excel保存到服务器端并下载
using MongoDB.Bson; using Newtonsoft.Json.Linq; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; ...
- 大数据mapreduce二分法ip定位之Python实现
ip定位数据大约12M,采用-chacheFile 分发 文件来源https://pan.baidu.com/s/1J0pwTafHgt4T0k3vV_gC-A 格式大致格式如下: 0.0.0.0 0 ...
- JAVA覆写Request过滤XSS跨站脚本攻击
注:本文非本人原著. demo的地址:链接:http://pan.baidu.com/s/1miEmHMo 密码:k5ca 如何过滤Xss跨站脚本攻击,我想,Xss跨站脚本攻击令人为之头疼.为什么呢. ...
- Confluence 6 字符集编码的问题解决
如果你的 Confluence 站点的字符集没有被正确配置,你可能会遇到下面的问题: Non-ASCII 字符将会显示为问号(?) Non-ASCII 字符集的页面链接将不能工作 单一字符将会被显示为 ...
- Confluence 6 数据库 JDBC 驱动
本页面提供了支持的数据库的所有 JDBC 驱动下载链接. 基于许可证的原因,我们没有将 MySQL 或 Oracle 的数据库驱动整合到 Confluence 中,因此你需要在 Confluence ...
- js数组的实例方法sort() 排序方法的运用,不再只是.sort()
1, sort() 不传回调函数的话,默认按照字母顺序(字符编码)的顺序进行排序. 2, sort() 通过传回调函数来控制从小到大的排序还是从大到小的排序: var arr = [1,23,5,6, ...
- 浅谈java中bigInteger用法
1.赋值: BigInteger a=new BigInteger("1"); BigInteger b=BigInteger.valueOf(1); 2.运算: ① add(); ...
- JWT实战
JWT实战 2018年03月02日 22:36:21 阅读数:129 JWT认证流程 先来回顾下JWT的流程,jwt是存储在客户端的,服务器不需要存储jwt;客户端每次发送请求时携带token,然后到 ...