JAVA读取和写入
ctl文件换行使用
import java.io.*;
import java.util.regex.Pattern;
/**
* 文件读取和写入
*/
public class FileReader {
public static void main(String[] args) {
//初始文件
File f =new File("D:\\11.txt");
BufferedReader bufferedReader = null;
StringBuffer buffer = null;
try {
//读取文件
bufferedReader= new BufferedReader(new java.io.FileReader(f));
//存取数据
buffer = new StringBuffer();
String temp = "";
//匹配开头"21
Pattern pattern = Pattern.compile("^\"21");
//按行读取
while ((temp=bufferedReader.readLine())!=null){
//if(!temp.startsWith("21")){
//匹配部分
if(!pattern.matcher(temp).find()){
//匹配全部
//if(!pattern.matcher(temp).matches()){
buffer.append("\\r\\n").append(temp);
} else{
buffer.append("\n").append(temp);
}
}
//输出日志
System.out.println(buffer.toString());
}catch (IOException e){
e.printStackTrace();
}finally {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//输出文件
File file = new File("D:\\22.txt");
FileOutputStream fileOutputStream =null;
try {
//判断文件存在否
if (!file.exists()){
file.createNewFile();
}
fileOutputStream = new FileOutputStream(file);
//写入文件
fileOutputStream.write(buffer.toString().getBytes());
}catch (IOException e){
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}