简体   繁体   English

在 Java 中读取/打开文本文件

[英]Reading/Opening a text file in Java

I know that there are probably hundreds of posts dealing with this exact question, but for the life of me, I cannot figure anything out.我知道可能有数百个帖子处理这个确切的问题,但对于我的生活,我无法弄清楚任何事情。 I have this "Open" case in this program I have committed myself to finishing, as a beginning Java exercise.我在这个程序中有这个“开放”案例,我已经承诺完成,作为一个开始 Java 练习。 I've gotten the Save function to work, but looking at that gets me no closer to trying my problem.我已经让 Save function 工作了,但是看着它让我离尝试我的问题更近了一步。 Here is my code.这是我的代码。

if(arg.equals(Editor.fileLabels[0])){
    if(Editor.VERBOSE)
    System.err.println(Editor.fileLabels[0] + 
               " has been selected");
    filedialog = new FileDialog(editor, "Open File Dialog", FileDialog.LOAD); 
    filedialog.setVisible(true);
    if(Editor.VERBOSE){ 
    System.err.println("Exited filedialog.setVisible(true);");
    System.err.println("Open file = " + filedialog.getFile());
    System.err.println("Open directory = " + filedialog.getDirectory()); 
    }

}

I have tried solutions before writing this question;在写这个问题之前我已经尝试过解决方案; however, all of the examples I've seen are separate methods of their own.但是,我看到的所有示例都是它们自己的单独方法。 Any help would be appreciated.任何帮助,将不胜感激。 :) :)

Whatever UI framework you are using, you will only have results of file dialog available after it was closed by user.无论您使用什么 UI 框架,只有在用户关闭文件对话框后才能获得文件对话框的结果。 In your case, you have shown dialog and immediately expect directory and file be available.在您的情况下,您已显示对话框并立即期望目录和文件可用。 It's not going to happen as dialog is probably still open.这不会发生,因为对话框可能仍处于打开状态。

Also it's all based on my guesses since you didn't really tell what is wrong and what you expect.此外,这一切都基于我的猜测,因为您并没有真正说出问题所在以及您的期望。

public class FileReadWrite {公共 class FileReadWrite {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    try {
        // Open the file that is the first 
        // command line parameter
        FileInputStream fstream = new  FileInputStream("Path for the file/filename.txt");

        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        File file = new File("Path for the file/filename.txt");
        Writer writer = new BufferedWriter(new FileWriter(file));


        //Read File Line By Line
        while ((strLine = br.readLine()) != null)   {
            // Print the content on the console
            System.out.println (strLine);
            String[] words = strLine.split("\\s+");
            String revrseStrline="";
            for(int i=words.length-1;i>=0; i-- )
            {
                revrseStrline+=words[i]+" ";
            }

            writer.write(revrseStrline);
            writer.write(System.getProperty("line.separator"));

          }

         // Close the input stream
        in.close();
        writer.close();
    } catch (Exception e) { // Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM