如何读取并展示 .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
类。
您可以通过以下方式从文件创建一个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类的完整规范。 它将有助于您处理其他任何想要进行的操作。
通常情况下:
FileInputStream
。InputStreamReader
,将输入流包装起来,并指定正确的编码方式。InputStreamReader
周围创建一个BufferedReader
,这样更容易一次读取一行。readLine
返回null)。如果您需要比这更详细的帮助,请在问题中更具体说明。
我喜欢这段代码,用它来将一个文件加载到一个字符串中:
File file = new File("/my/location");
String contents = new Scanner(file).useDelimiter("\\Z").next();
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 + "'");
}
}
}
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);
}
import org.apache.commons.io.FileUtils;
String data = FileUtils.readFileToString(new File("..."), "UTF-8");
System.out.println(data);
:-)
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();
}
}
你很可能想要使用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);