Java IO流相关知识代码解析

时间:2022-10-19 19:10:31

一、IO流的分类

字符流

Reader
InputStreamReader(节点流)
BufferedReader(处理流)
Writer
OutputStreamWriter(节点流)
BufferedWriter(处理流)
PrintWriter

字节流

InputStream
FileInputStream(节点流)
BufferedInputStream(处理流)
ObjectInputStream(处理流)
PrintStream
OutputStream
FileOutputStream(节点流)
BufferedOutputStream(处理流)
ObjectOutputStream(处理流)

断点处理的流

RandomAccessfile

二、IO流的用法

1、转换流的用法

?
1
2
3
4
FileInputStream in = new FileInputStream(newFile(""));    
 Readerreader = new InputStreamReader(in);//字节转字符
 FileOutputStreamout = new FileOutputStream(newFile(""));
 Writer writer = new OutputStreamWriter(out);//字符转字节

2、对象序列化,对象需要实现Serializable接口

?
1
2
3
4
5
6
7
8
9
FileOutputStreamfileOutputStream = new FileOutputStream("C:\\Users\\lx\\Desktop\\Record.txt");
ObjectOutputStreamobjectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(object);//向指定文件写入对象object
objectOutputStream.close();
 
FileInputStreamfileInputStream = new FileInputStream("C:\\Users\\lx\\Desktop\\Record.txt");
ObjectInputStreamobjectInputStream = new ObjectInputStream(fileInputStream);
object = objectInputStream.readObject();//读取得到对象object
fileInputStream . lose();

3、断点的运用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class Copy extends Thread{
    //可以利用多线程实现拷贝 
    longstart;
    longend;
    Filesorce;
    Filetargetdir;
    publicCopy() {
    }
    publicCopy(longstart,long end, File sorce, File targetdir) {
        //利用构造方法传递需要拷贝的长度,拷贝开始位置,以及目标文件和源文件
        super();
        this.start= start;
        this.end= end;
        this.sorce= sorce;
        this.targetdir= targetdir;
    }
    @Override
       publicvoid run(){
        try{
            RandomAccessFilesouceRaf = new RandomAccessFile(sorce,"r");
            RandomAccessFiletargetRaf = new RandomAccessFile(newFile(targetdir,sorce.getName()),"rw");
            souceRaf.seek(start);
            targetRaf.seek(start);
            intlen= 0;
            byte[]bs = new byte[1024];
            longseek;
            System.out.println(start+"---->"+end+this.getName());
            while((len= souceRaf.read(bs))!=-1){
                targetRaf.write(bs, 0, len);
                seek= souceRaf.getFilePointer();
                //获取断点位置
                if(seek== end){
                    break;
                }
            }
            targetRaf.close();
            souceRaf.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4、字节流的用法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class Test_InputStream {
    //利用字节流获取文本文件内容,但是容易出现问题
    /* 
  //可能出现int长度越界
  public static void main(String[] args) throws IOException {
    InputStream inputStream = new FileInputStream(new File("C:\\Users\\lx\\Desktop\\test\\33.txt"));
    byte[] b = new byte[inputStream.available()]; 
    inputStream.read(b);
    String str = new String(b);
    System.out.println(str);
  }
*/
    //可能出现乱码   
    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\lx\\Desktop\\test\\33.txt");
        InputStream inputStream = new FileInputStream(file);
        //统计每次读取的实际长度
        int len = 0;
        //声明每次读取1024个字节
        byte[] b = new byte[2];
        StringBuffer sBuffer = new StringBuffer();
        while((len=inputStream.read(b))!=-1){
            sBuffer.append(new String(b,0,len));
        }
        System.out.println(sBuffer.toString());
    }
}
//利用字节流拷贝文件
public void copy(File sourceFile, File targetDir) {
    //
    FileInputStreamfileInputStream = null;
    FileOutputStreamfileOutputStream = null;
    fileInputStream= new FileInputStream(sourceFile);
    FiletargetFile = new File(targetDir,sourceFile.getName());
    fileOutputStream= new FileOutputStream(targetFile);
    byte[]b = new byte[1024];
    intlen = 0;
    while((len= fileInputStream.read(b)) != -1) {
        fileOutputStream.write(b, 0, len);
    }
}

5、缓存字符流的用法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
publicstatic void main(String[] args) throws IOException {
    //缓存字符流实现写入文件
    InputStreamin = System.in;
    Readerreader = new InputStreamReader(in);
    BufferedReaderbr = new BufferedReader(reader);
    BufferedWriterbw = new BufferedWriter(new FileWriter(new File("src/1.txt")));
    Strings="";
    while((s=br.readLine())!=null) {
        bw.write(s);
        bw.newLine();
        bw.flush();
        //字符流千万不要忘了flush!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    }
}

总结

以上就是本文关于Java IO流相关知识代码解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://blog.csdn.net/qq_24065713/article/details/76407833