【问题标题】:Java. BufferWriter爪哇。缓冲写入器
【发布时间】:2017-08-25 17:41:14
【问题描述】:

我是 Java 新手,目前正在学习如何读取文件、执行工作并将结果写入文件。我的代码可以无错误地运行,但我不知道为什么输出文件不能从控制台打印出任何内容(它必须打印每行的最后一个字)。谁能指出我如何修复以使其正常工作? 非常感谢。 这是我的代码。

import javax.swing.JButton;
import javax.swing.JFileChooser;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class ReadingWriting {
    private static BufferedReader reader;
    private static BufferedWriter writer;
    private static String currentLine;
    private static String result;


public static void main(String[] arg) {
    File inputFile=null;
    JButton open= new JButton();
    JFileChooser jfc = new JFileChooser();
    jfc.setCurrentDirectory(new java.io.File("."));
    jfc.setDialogTitle("ReadingWriting");
     if (jfc.showOpenDialog(open) == JFileChooser.APPROVE_OPTION){  
            inputFile = jfc.getSelectedFile();

            try {
            reader = new BufferedReader(new FileReader(inputFile));


            }catch (FileNotFoundException e){
            System.out.println("The file was not found");
            System.exit(0);
            }
     }
     try {
            Scanner input = new Scanner(inputFile);

            while ((currentLine = reader.readLine()) != null){

            currentLine = currentLine.trim();
            String[] wordList = currentLine.split("\\s+");
            String result =wordList[wordList.length-1];
            System.out.println(result+"");
            }

            input.close();
            reader.close();
            }catch (IOException e2){
            System.out.println("The file was not found");
            System.exit(0);
           }

     try{
            File outFile = new File("src/result.txt");
        writer= new BufferedWriter(new FileWriter(outFile));
        writer.write(result+"");
        writer.close();
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }  

}        
}

输入文件(hello.txt):

你好世界

他是个好孩子

345 123 46 765

输出文件(result.txt):

世界

男孩

765

我目前的 result.txt 文件:

【问题讨论】:

  • 您将result 隐藏为try 块内的局部变量。
  • 另外,您正在将最后一个result 写入文件。您需要为每行的最后一个单词创建一个列表或数组,然后将其写入输出文件。

标签: java file bufferedreader bufferedwriter


【解决方案1】:

您的代码存在一些问题..

  1. 你创建了两次result(一个类变量和一个局部变量)

    private static String result;
    ...
    String result =wordList[wordList.length-1];
    

就像 EJP 提到的那样。 String result =wordList[wordList.length-1]; 的局部变量实际上隐藏了类变量 result。因此,一旦离开 while 循环的范围,数据实际上就会丢失。

在 while 循环之外,您正在访问仍然为空的类变量 resullt


  1. 目前,您的 while 循环将仅存储文本文件中最后一句的最后一个单词。

你可以边读边写结果:

while ((currentLine = reader.readLine()) != null){
    currentLine = currentLine.trim();
    String[] wordList = currentLine.split("\\s+");
    String result =wordList[wordList.length-1];
    writer.write(result);    //write to file straight
}

或者,您可以先保存所有句子的最后一个单词,然后单独执行写作。用户 Tima 也提到了这一点:

ArrayList<String> resultList = new ArrayList<String>();
...

while ((currentLine = reader.readLine()) != null){
    currentLine = currentLine.trim();
    String[] wordList = currentLine.split("\\s+");
    resultList.add(wordList[wordList.length-1]);    //add to a list
}

...

//Perform your writing 
for(String s : resultList)
    writer.write(s);

【讨论】:

  • 太棒了。感谢您的帮助。
【解决方案2】:

您在此处创建了一个结果作为临时变量。

String[] wordList = currentLine.split("\\s+");
String result = wordList[wordList.length - 1];
System.out.println(result + "");

您必须将所有读取的行存储在数组中,然后再写入 稍后到另一个文件。

或者

你可以从一个文件中读取一行,然后在另一个文件中写入一行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2021-10-08
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 2023-03-04
    相关资源
    最近更新 更多