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+"\r\n";
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();
}
}
}
java对文件的一行行写入,一行读取
最新推荐文章于 2024-07-25 02:04:30 发布