简体   繁体   English

Java:无法读取文件

[英]Java: Having trouble reading from a file

import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;


public class TextFile {

private static void doReadWriteTextFile() {

    try {

        // input/output file names
        String inputFileName  = "README_InputFile.rtf";

        // Create FileReader Object
        FileReader inputFileReader   = new FileReader(inputFileName);

        // Create Buffered/PrintWriter Objects
        BufferedReader inputStream   = new BufferedReader(inputFileReader);


        while ((inLine = inputStream.readLine()) != null) {
            System.out.println(inLine);
        }

        inputStream.close();

    } catch (IOException e) {

        System.out.println("IOException:");
        e.printStackTrace();

    }

}

public static void main(String[] args) {
    doReadTextFile();
}

}

I'm just learning Java, so take it easy on me. 我只是在学习Java,所以请放轻松。 My program's objective is to read a text file and output it into another text file in reverse order. 我程序的目标是读取一个文本文件,然后以相反的顺序将其输出到另一个文本文件中。 The problem is the professor taught us to to deal with strings and reverse it and such, but nothing about importing/exporting files. 问题是教授教我们如何处理字符串并反转字符串等,但是与导入/导出文件无关。 Instead, he gave us the following sample code which should import a file. 相反,他给了我们以下示例代码,该示例代码应导入文件。 The file returns 3 errors: The first two deal with inLine not being a symbol on lines 24 and 25. The last cannot find the symbol doReadTextFile on line 40. 该文件返回3个错误:前两个处理inLine不是第24和25行上的符号。最后一个找不到在第40行上的符号doReadTextFile。

I have no idea how to read this file and make the necessary changes to reverse and output into a new file. 我不知道如何读取此文件并进行必要的更改以反转并输出到新文件中。 Any help is hugely appreciated. 任何帮助深表感谢。

I also had to change the file type from .txt to .rtf. 我还必须将文件类型从.txt更改为.rtf。 I'm not sure if that affects how I need to go about this. 我不确定这是否会影响我的处理方式。

EDIT I defined inLine and fixed the doReadWritetextFile naming error, which fixed all my compiling errors. 编辑我定义了inLine并修复了doReadWritetextFile命名错误,该错误修复了我所有的编译错误。 Any help on outputting into new file still appreciated! 在输出到新文件的任何帮助仍然赞赏!

I'm also aware he gave me bad sample code. 我也知道他给了我不好的示例代码。 It's supposed to be so we can learn troubleshooting, but with no working code to go off of and very extremely knowledge of the language, it's very difficult to see what's wrong. 可以这样,我们可以学习疑难解答,但是由于没有可用的工作代码并且对语言非常了解,所以很难看出问题所在。 Thanks for the help! 谢谢您的帮助!

The good practice will be to use a BufferedFileReader 好的做法是使用BufferedFileReader

BufferedFileReader bf = new BufferedFileReader(new FileReader(new File("your_file.your_extention")));

Then you can read lines in your file : 然后,您可以读取文件中的行:

// Initilisation of the inLine variable...
String inLine = null;

while((inLine = bf.readLine()) != null){
    System.out.println(inLine);
}

To output a file, you can use StringBuilder to hold the file contents: 要输出文件,可以使用StringBuilder来保存文件内容:

private static void doReadWriteTextFile() 
{
    ....
    StringBuilder sb = new StringBuilder();
    while ((inLine = inputStream.readLine()) != null) 
    {
        sb.append(inline);
    }

     FileWriter writer = new FileWriter(new File("C:\\temp\\test.txt"));
     BufferedWriter bw = new BufferedWriter(writer);
     w.write(sb.toString());
     bw.close();
}

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

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