import java.io.*;
/**
* 读取txt文件
* 一次读一行
* @author ylq
*
*/
public class oput {
public static void path( String input,String output) throws IOException {
//文件路径
//String filePath="F:\\9ebang\\data\\gis\\output\\1xian50.txt";
String filePath=input;
File file=new File(filePath);
BufferedReader reader = null;//字符输入流进行读取操作读取
String tempString = null;//每一行的内容
int line =1;//行号
FileWriter fw = new FileWriter(output);
try {
//输入字节流,FileInputStream主要用来操作文件输入流
FileInputStream intput = new FileInputStream(file);
reader = new BufferedReader(new InputStreamReader(intput));
while ((tempString = reader.readLine()) != null) {
String[] split = tempString.split("\t");
System.out.println(split.length);
if (split.length==3){
System.out.println("三个"+split[0]+","+split[1]+","+split[2]);
fw.write("["+"\""+split[0]+"\""+","+"\""+split[1]+"\""+","+"\""+split[2]+"\""+"]"+","+"\n");
}else {
fw.write("["+"\""+split[0]+"\""+","+"\""+split[1]+"\""+","+"\""+split[2]+"\""+","+"\""+split[3]+"\""+"]"+","+"\n");
}
}
fw.flush();
fw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws Exception {
//1号线
String input1="F:\\9ebang\\data\\gis\\output\\1xian50.txt";
String output1="F:\\9ebang\\data\\gis\\output\\1xian50apk.txt";
//5号线
String input5="F:\\9ebang\\data\\gis\\output\\5xian50.txt";
String output5="F:\\9ebang\\data\\gis\\output\\5xian50apk.txt";
path(input5,output5);
}
}
JAVA中IO流读取和写入样例
最新推荐文章于 2022-05-11 22:57:43 发布