java 文件的IO 写与读

直接上代码:

1,写文件

import java.io.FileWriter;
import java.io.IOException;

//需求:将一些文字存储到硬盘一个文件中。

public class FileWriterDemo {
    private static final String LINE_SEPARATOR =System.getProperty("line.separator") ;//是换行的"标准姿势"

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

        //创建一个可以往文件中写入字符数据的字符输出流对象。
		/*
		 * 既然是往一个文件中写入文字数据,那么在创建对象时,就必须明确该文件(用于存储数据的目的地)。
		 *
		 * 如果文件不存在,则会自动创建。
		 * 如果文件存在,则会被覆盖。
		 *
		 * 如果构造函数中加入true,可以实现对文件进行续写!
		 */
        FileWriter fw = new FileWriter("demo.txt",true);//对LINE_SEPARATOR使用alt+enter,选择create constant field "LINE_SEPARATOR"

		/*
		 * 调用Writer对象中的write(string)方法,写入数据。
		 *
		 * 其实数据写入到临时存储缓冲中。
		 *
		 */
        fw.write("abcde"+LINE_SEPARATOR+"hahaha");
        fw.write("xixi");

		/*
		 * 进行刷新,将数据直接写到目的地中。
		 */

//		fw.flush();

		/*
		 * 关闭流,关闭资源。在关闭前会先调用flush刷新缓冲中的数据到目的地。
		 */
        fw.close();

//		fw.write("haha");// java.io.IOException: Stream closed



    }

}

1.1写文件异常 

import java.io.FileWriter;
import java.io.IOException;

public class IOExceptionDemo {

	private static final String LINE_SEPARATOR = System
			.getProperty("line.separator");

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) {

		FileWriter fw = null;
		try {

			fw = new FileWriter("k:\\demo.txt");

			fw.write("abcde" + LINE_SEPARATOR + "hahaha");

		} catch (IOException e) {
			System.out.println(e.toString());
		} finally {
			if (fw != null)//如果文件不能创建,是无法关闭的
				try {
					fw.close();
				} catch (IOException e) {
					// code....
					throw new RuntimeException("关闭失败");
				}
		}

	}

}

2.读文件

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

//需求:读取一个文本文件。将读取到的字符打印到控制台.

public class FileReaderDemo {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {

		
		//1,创建读取字符数据的流对象。
		/*
		 * 在创建读取流对象时,必须要明确被读取的文件。一定要确定该文件是存在的。 
		 * 
		 * 用一个读取流关联一个已存在文件。 
		 */
		FileReader fr = new FileReader("demo.txt");
		
		int ch = 0;
		
		while((ch=fr.read())!=-1){  //当没有读到末尾时,jvm设置字符串末尾为-1
			System.out.println((char)ch);
		}
		
		/*
		//用Reader中的read方法读取字符。
		int ch = fr.read();
		System.out.println((char)ch);
		int ch1 = fr.read();
		System.out.println(ch1);
		int ch2 = fr.read();
		System.out.println(ch2);
		*/
		
		fr.close();
	}

}

2.2一次性读完

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

//需求:读取一个文本文件。将读取到的字符打印到控制台.

public class FileReaderDemo2 {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {

        FileReader fr = new FileReader("demo.txt");

		/*
		 * 使用read(char[])读取文本文件数据。
		 *
		 * 先创建字符数组。
		 */
        char[] buf = new char[1024];

        int len = 0;

        while((len=fr.read(buf))!=-1){
            System.out.println(new String(buf,0,len));
        }

		/*
		int num = fr.read(buf);//将读取到的字符存储到数组中。
		System.out.println(num+":"+new String(buf,0,num));
		int num1 = fr.read(buf);//将读取到的字符存储到数组中。
		System.out.println(num1+":"+new String(buf,0,num1));
		int num2 = fr.read(buf);//将读取到的字符存储到数组中。
		System.out.println(num2+":"+new String(buf));
		*/




        fr.close();
    }

}

代码来  自传智播客

猜你喜欢

转载自blog.csdn.net/nsjlive/article/details/83216167
今日推荐