Java 文件读写操作

1. 对于文本文件的读写

1.1. 读取文本文件

package com.martinwangjun.io;

import java.io.FileReader;
import java.io.IOException;

public class FileReaderTest {

    // 注意,需要抛出IOException或在代码中使用try...catch...
    public static void main(String[] args) throws IOException 
    {
        // 对于eclipse,注意实际的路径,我们这里用了package
        FileReader fileReader = new FileReader(
            "./src/com/martinwangjun/io/FileReaderTest.java");
        char[] buffer = new char[1024];
        int hasReader = 0;
        while ((hasReader = fileReader.read(buffer)) > 0) {
            System.out.println(new String(buffer, 0, hasReader));
        }
        // 这里其实写得不严谨,最好使用Java 7的AutoClosable
        // 或者finally中关闭资源
        fileReader.close();
    }
}

1.2. 写入文本文件

package com.martinwangjun.io;

import java.io.FileWriter;

public class FileWriterTest {

    public static void main(String[] args) {
        // 这里用来Java 7的AutoClosable的特性,会自动关闭资源。
        try(FileWriter fileWriter = new FileWriter(
                "src/com/martinwangjun/io/poem.txt")) {
            fileWriter.write("先令\r\n");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}

2. 二进制文件

package com.martinwangjun.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamTest {

    public static void main(String[] args) throws IOException{
        // 使用了Java 7的自动关闭机制
        try(FileInputStream fileInputStream = new FileInputStream(
                "src/com/martinwangjun/io/01.jpg");
            FileOutputStream fileOutputStream = new FileOutputStream(
                "src/com/martinwangjun/io/02.jpg");
                ){
            byte[] buffer = new byte[1024];
            int hasRead = 0;
            while ((hasRead = fileInputStream.read(buffer)) > 0) {
                fileOutputStream.write(buffer, 0, hasRead);
            }
            buffer = "Powered By Martin".getBytes();
            fileOutputStream.write(buffer, 0, 
                "Powered By Martin".length());
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

3. 利用包装流

3.1. 利用PrintStream流来包装FileOutputStream

package com.martinwangjun.io;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class PrintStreamTest {

    public static void main(String[] args) throws IOException {

        try(
            FileOutputStream fos = new FileOutputStream(
                    "src/com/martinwangjun/io/test.txt");
            PrintStream ps = new PrintStream(fos))
        {
            // 这样的话不用考虑怎么写入文件,当成普通的打印一样使用
            ps.println("普通字符串");
            ps.println(new PrintStreamTest());
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

4. 获取用户输入

package com.martinwangjun.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class KeyinTest {

    public static void main(String[] args) throws IOException{
        InputStreamReader reader = null;
        BufferedReader bufferedReader = null;
        try {
            reader = new InputStreamReader(System.in);
            bufferedReader = new BufferedReader(reader);

            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                if(line.toLowerCase().equals("exit")){
                    System.exit(1);
                }
                System.out.println(line);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if(reader != null){
                try {
                    reader.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            if(bufferedReader != null){
                try {
                    bufferedReader.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

}

5. 添加而不是覆盖

package com.martinwangjun.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class BufferedWriteTextFile {

    public static void main(String[] args) throws Exception {
        try (
            // 这里用了true,表示可以Append
            BufferedWriter bufferedWriter = new BufferedWriter(
                new OutputStreamWriter(
                    new FileOutputStream(
                        "src/com/martinwangjun/io/output.txt", true)));
            BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(System.in))) 
        {
            String line = null;
            while( ! (line = bufferedReader.readLine()).equals("")){
                bufferedWriter.write(line);
                bufferedWriter.newLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值