Java 从测试文件写入多个查询 publicstaticvoidmain(字符串[]args){ ArrayList studentTokens=新的ArrayList(); ArrayList studentId=新的ArrayList(); 试一试{ //打开第一个文件 //命令行参数 FileInputStream fstream=新的FileInputStream(新文件(“file1.txt”); BufferedReader br=新的BufferedReader(新的InputStreamReader(fstream,“UTF8”)); 弦斯特林; //逐行读取文件 而((strLine=br.readLine())!=null){ strLine=strLine.trim(); 如果((strLine.length()!=0)和(!strLine.contains(“#”){ 字符串[]students=strLine.split(\\s+); 添加(学生[TOKEN_列]); 添加(学生[学生ID列]); } } 对于(int i=0;i
但在调试时,它显示所有值都被正确替换 如果在调试代码时发现值被替换,但文件中缺少这些值,我建议您刷新输出流。您正在关闭Java 从测试文件写入多个查询 publicstaticvoidmain(字符串[]args){ ArrayList studentTokens=新的ArrayList(); ArrayList studentId=新的ArrayList(); 试一试{ //打开第一个文件 //命令行参数 FileInputStream fstream=新的FileInputStream(新文件(“file1.txt”); BufferedReader br=新的BufferedReader(新的InputStreamReader(fstream,“UTF8”)); 弦斯特林; //逐行读取文件 而((strLine=br.readLine())!=null){ strLine=strLine.trim(); 如果((strLine.length()!=0)和(!strLine.contains(“#”){ 字符串[]students=strLine.split(\\s+); 添加(学生[TOKEN_列]); 添加(学生[学生ID列]); } } 对于(int i=0;i,java,bufferedreader,filewriter,Java,Bufferedreader,Filewriter,但在调试时,它显示所有值都被正确替换 如果在调试代码时发现值被替换,但文件中缺少这些值,我建议您刷新输出流。您正在关闭FileWriter,而不调用flush()。close()方法将其调用委托给底层的streamncoder,后者也不会刷新流 public static void main(String[] args) { ArrayList<String> studentTokens = new ArrayList<String>(); Arr
FileWriter
,而不调用flush()
。close()
方法将其调用委托给底层的streamncoder
,后者也不会刷新流
public static void main(String[] args) {
ArrayList<String> studentTokens = new ArrayList<String>();
ArrayList<String> studentIds = new ArrayList<String>();
try {
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream(new File("file1.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(fstream, "UTF8"));
String strLine;
// Read File Line By Line
while ((strLine = br.readLine()) != null) {
strLine = strLine.trim();
if ((strLine.length()!=0) && (!strLine.contains("#"))) {
String[] students = strLine.split("\\s+");
studentTokens.add(students[TOKEN_COLUMN]);
studentIds.add(students[STUDENT_ID_COLUMN]);
}
}
for (int i=0; i<studentIds.size();i++) {
File file = new File("query.txt"); // The path of the textfile that will be converted to csv for upload
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "", oldtext = "";
while ((line = reader.readLine()) != null) {
oldtext += line + "\r\n";
}
reader.close();
String newtext = oldtext.replace("sanid", studentIds.get(i)).replace("salabel",studentTokens.get(i)); // Here the name "sanket" will be replaced by the current time stamp
FileWriter writer = new FileWriter("final.txt",true);
writer.write(newtext);
writer.close();
}
fstream.close();
br.close();
System.out.println("Done!!");
} catch (Exception e) {
e.printStackTrace();
System.err.println("Error: " + e.getMessage());
}
}
试试这个
public void close() throws IOException {
se.close();
}
这样就可以了。所以
System.out.println(newtext)
打印正确的数据,但是final.txt
不包含相同的内容?使用stringbuffer-append方法
writer.flush();
writer.close();