当前位置 : 主页 > 编程语言 > c++ >

java读写txt

来源:互联网 收集:自由互联 发布时间:2021-06-30
java读写txt import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class Text {public void JavaToText(String text) {FileWriter fw = null;BufferedWr
java读写txt
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Text {
	public void JavaToText(String text) {
		FileWriter fw = null;
		BufferedWriter bw = null;
		try {
			fw = new FileWriter("/home/text.txt");
			bw = new BufferedWriter(fw);
			bw.write(text);
			bw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String TextToJava() {
		FileReader fr = null;
		BufferedReader br = null;
		String text = null;
		try {
			fr = new FileReader("/home/text.txt");
			br = new BufferedReader(fr);
			text = br.readLine();
			br.close();
			fr.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return text;
	}
网友评论