如何从.txt文件中读取和显示数据?
答案 0 :(得分:43)
BufferedReader in = new BufferedReader(new FileReader("<Filename>"));
然后,你可以使用in.readLine();一次读一行。要读到最后,请写一个while循环:
String line;
while((line = in.readLine()) != null)
{
System.out.println(line);
}
in.close();
答案 1 :(得分:22)
如果你的文件是严格的文本,我更喜欢使用java.util.Scanner
类。
您可以通过以下方式从文件中创建Scanner
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
后,您可以使用以下命令将字符串打印到命令行:
System.out.print(aString);
System.out.println(aString);
发布的链接包含Scanner类的完整规范。帮助您完成其他任何您想要做的事情会很有帮助。
答案 2 :(得分:9)
一般来说:
FileInputStream
。InputStreamReader
,指定正确的encoding InputStreamReader
周围创建BufferedReader
,这样可以更方便地一次读取一行。readLine
返回null)如果您需要更多帮助,请在您的问题中更具体。
答案 3 :(得分:7)
我喜欢这段代码,用它将文件加载到一个字符串中:
File file = new File("/my/location");
String contents = new Scanner(file).useDelimiter("\\Z").next();
答案 4 :(得分:1)
下面是您可能尝试使用scanner类尝试读取文件并在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 + "'");
}
}
}
答案 5 :(得分:0)
如果您想采用一些快捷方式,可以使用Apache Commons IO:
import org.apache.commons.io.FileUtils;
String data = FileUtils.readFileToString(new File("..."), "UTF-8");
System.out.println(data);
: - )
答案 6 :(得分:0)
使用encoding UTF-8
从.text文件中读取行:
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();
答案 7 :(得分:0)
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();
}
}
答案 8 :(得分:0)
在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);
}
答案 9 :(得分:-2)
您很可能希望使用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构造函数抛出的一些异常,但这些是特定于项目的实现细节。