简体   繁体   中英

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. I've gotten the Save function to work, but looking at that gets me no closer to trying my problem. 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. 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 {

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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