JAVA缓冲写入程序

JAVA缓冲写入程序,java,file,bufferedreader,bufferedwriter,Java,File,Bufferedreader,Bufferedwriter,我是java新手,目前正在学习如何读取文件、执行工作以及将结果写入文件。我的代码可以无错误运行,但我不知道为什么输出文件不能从控制台打印任何内容(它必须打印每行的最后一个字)。有人能告诉我怎样修理才能使它工作吗? 非常感谢你。 这是我的密码 import javax.swing.JButton; import javax.swing.JFileChooser; import java.io.BufferedReader; import java.io.BufferedWriter; import

我是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):

你好,世界

他是个好孩子

34512346765

输出文件(result.txt):

世界

男孩

765

我当前的result.txt文件:


null

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

String[] wordList = currentLine.split("\\s+");
String result = wordList[wordList.length - 1];
System.out.println(result + "");
您必须将所有读取行存储在数组中,然后再写入 稍后转到另一个文件


您可以从一个文件中读取行,然后再将行写入另一个文件。

您的代码有一些问题

  • 您创建了两次
    result
    (一个类变量和一个局部变量)

  • 就像EJP提到的。您的局部变量
    String result=wordList[wordList.length-1]实际上是对类变量
    结果进行阴影处理
    。因此,一旦离开while循环的范围,数据实际上就丢失了

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


  • 当前,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);
    
    ArrayList resultList=new ArrayList();
    ...
    while((currentLine=reader.readLine())!=null){
    currentLine=currentLine.trim();
    字符串[]单词列表=currentLine.split(\\s+);
    add(wordList[wordList.length-1]);//添加到列表
    }
    ...
    //完成你的写作
    for(字符串s:resultList)
    作者:写;
    
    您正在将
    结果作为
    try
    块内的局部变量进行阴影处理。此外,您正在将最后一个
    结果写入文件。您需要为每行的最后一个字创建一个列表或数组,然后将其写入输出文件。太好了。谢谢你的帮助。
    
    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);