从.txt文件读取和显示数据

你如何阅读和显示来自.txt文件的数据?

BufferedReader in = new BufferedReader(new FileReader("<Filename>")); 

然后,你可以使用in.readLine(); 一次只读一行。 要阅读直到最后,写一个while循环,如下所示:

 String line; while((line = in.readLine()) != null) { System.out.println(line); } in.close(); 

如果你的文件是严格的文本,我更喜欢使用java.util.Scanner类。

您可以通过以下方式创buildScanner

 Scanner fileIn = new Scanner(new File(thePathToYourFile)); 

然后,您可以使用以下方法从文件中读取文本:

 fileIn.nextLine(); // Reads one line from the file fileIn.next(); // Reads one word from the file 

而且,你可以检查是否还有更多的文本:

 fileIn.hasNext(); // Returns true if there is another word in the file fileIn.hasNextLine(); // Returns true if there is another line to read from the file 

读完文本并保存到String ,可以使用以下命令将string打印到命令行:

 System.out.print(aString); System.out.println(aString); 

发布的链接包含Scanner类的完整规范。 帮助你完成你可能想做的任何事情将是有帮助的。

一般来说:

  • 为该文件创build一个FileInputStream
  • 创build一个包装inputstream的InputStreamReader ,指定正确的编码
  • 可以select在InputStreamReader周围创build一个BufferedReader ,这样一次只读一行就简单了。
  • 读直到没有更多的数据(例如, readLine返回null)
  • 随时显示数据或将其caching以备后用。

如果您需要更多帮助,请在您的问题中更具体。

我喜欢这段代码,用它来将一个文件加载到一个string中:

 File file = new File("/my/location"); String contents = new Scanner(file).useDelimiter("\\Z").next(); 

以下是您可能尝试读取文件并使用扫描仪类在java中显示的代码。 代码将从用户读取文件名并打印数据(记事本VIM文件)。

 import java.io.*; import java.util.Scanner; import java.io.*; public class TestRead { public static void main(String[] input) { String fname; Scanner scan = new Scanner(System.in); /* enter filename with extension to open and read its content */ System.out.print("Enter File Name to Open (with extension like file.txt) : "); fname = scan.nextLine(); /* this will reference only one line at a time */ String line = null; try { /* FileReader reads text files in the default encoding */ FileReader fileReader = new FileReader(fname); /* always wrap the FileReader in BufferedReader */ BufferedReader bufferedReader = new BufferedReader(fileReader); while((line = bufferedReader.readLine()) != null) { System.out.println(line); } /* always close the file after use */ bufferedReader.close(); } catch(IOException ex) { System.out.println("Error reading file named '" + fname + "'"); } } 

}

如果你想采取一些捷径,你可以使用Apache Commons IO :

 import org.apache.commons.io.FileUtils; String data = FileUtils.readFileToString(new File("..."), "UTF-8"); System.out.println(data); 

🙂

encoding UTF-8 .txt文件读取行:

 File krokiPath = new File("Records\Record"); BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(krokiPath), "UTF8")); while((r = in.readLine()) != null) { System.out.println(r); } in.close(); 
 public class PassdataintoFile { public static void main(String[] args) throws IOException { try { PrintWriter pw = new PrintWriter("C:/new/hello.txt", "UTF-8"); PrintWriter pw1 = new PrintWriter("C:/new/hello.txt"); pw1.println("Hi chinni"); pw1.print("your succesfully entered text into file"); pw1.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } BufferedReader br = new BufferedReader(new FileReader("C:/new/hello.txt")); String line; while((line = br.readLine())!= null) { System.out.println(line); } br.close(); } } 

在Java 8中,只需使用以下命令即可读取整个文件:

 public String read(String file) throws IOException { return new String(Files.readAllBytes(Paths.get(file))); } 

或者如果它的资源:

 public String read(String file) throws IOException { URL url = Resources.getResource(file); return Resources.toString(url, Charsets.UTF_8); } 

你很可能会想要使用FileInputStream类:

 int character; StringBuffer buffer = new StringBuffer(""); FileInputStream inputStream = new FileInputStream(new File("/home/jessy/file.txt")); while( (character = inputStream.read()) != -1) buffer.append((char) character); inputStream.close(); System.out.println(buffer); 

您还需要捕获read()方法和FileInputStream构造函数引发的一些exception,但这些是您的项目特定的实现细节。