Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
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 - Fatal编程技术网

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();