java对文件的一行行写入,一行读取

发布于:2021-11-29 19:39:29

package aaa;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

public class file {

static String path = "c:/aaar/";
static String fileName = "sb.txt";

public static void main(String[] args) {
// TODO Auto-generated method stub

File file = new File(path);
if(!file.exists()){
file.mkdirs();
}

file = null;
file = new File(path + fileName);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

//writeData("aaaa");
//writeData("bbbb");
writeData("SZD");

readFile();

}


public static void writeData(String ip) {
File file = new File(path+fileName);
ip = ip+"
";
try {
FileOutputStream fos = new FileOutputStream(file,true);
fos.write(ip.getBytes());
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static void readFile() {
FileReader fr;
try {
fr = new FileReader(path+fileName);
BufferedReader br=new BufferedReader(fr);
String line="";
while ((line=br.readLine())!=null) {
System.out.println(line);
}
br.close();
fr.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}


相关推荐

最新更新

猜你喜欢