import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ReadText {
public static void main(String[] args)
{
readTextContent();
}
public static void readTextContent()
{
try
{
File file = new File("E://test.txt");
FileInputStream fis = new FileInputStream(file);
String str = "";
byte[] bytes = new byte[1024];
int length = 0;
while ((length = fis.read(bytes)) != -1)
{
str += new String(bytes, 0, length);
}
System.out.println(str);
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
本文介绍了如何使用Java通过FileInputStream读取E盘'test.txt'文件的内容,重点展示了异常处理和文件I/O操作的实践。

被折叠的 条评论
为什么被折叠?



