Java BufferedReader未读取完整的.txt文件
我试图将一个中等大小的txt文件(65,00个单词)读入一个字符串,然后读入一个字符串数组。bufferedreader抛出“无法读取文件”捕获。当我清除它时,文本文件的一小部分内容显示在system.out中。我没有得到较小的文本文件的错误。我是个初学者,在缩小问题范围方面遇到了很多困难 为什么BufferedReader不接收整个文件?为什么会抛出“无法读取文件”错误Java BufferedReader未读取完整的.txt文件,java,bufferedreader,Java,Bufferedreader,我试图将一个中等大小的txt文件(65,00个单词)读入一个字符串,然后读入一个字符串数组。bufferedreader抛出“无法读取文件”捕获。当我清除它时,文本文件的一小部分内容显示在system.out中。我没有得到较小的文本文件的错误。我是个初学者,在缩小问题范围方面遇到了很多困难 为什么BufferedReader不接收整个文件?为什么会抛出“无法读取文件”错误 public class Main { public static void main(String[] args) {
public class Main {
public static void main(String[] args) {
final Guid Main = new Guid(); //creates instance of Guid
Main.mergeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
merge();
}
});
}
static void merge()
{
//read file and turn into string
String pathone = open(Guid.PathOne);
System.out.print(pathone);
//parse and format
//String[] oneArray = pathone.replace("\n"," ").split(" ");
//get pathtwo text
//String pathtwo = open(Guid.PathTwo);
//parse and format
//load into array
//compare array entries
//add new array entry
//sort array
//write array to paththree file
//for(int i=0; i<oneArray.length;i++)
//{
//System.out.println(oneArray[i]);
//}
}
public static String open(JTextArea Path)
{
String record = null;
FileReader frFile = null;
try {
frFile = new FileReader(Path.getText());//gets file from Path
BufferedReader brFile = new BufferedReader(frFile);//creates buffered reader
record = brFile.readLine() + "\n"; //gets contents of file and puts it into a string
brFile.mark(0);
while (brFile.read() != -1) //loop to read the rest of the text file
{
brFile.reset();
record = record + brFile.readLine() + "\n";
brFile.mark(0);
}
}
catch (FileNotFoundException e) //catch path is in error
{
JFrame frame = null;
JOptionPane.showMessageDialog(frame, "Could not find file.");
}
catch (IOException e) //catch if file cannot be read
{
JFrame frame = null;
JOptionPane.showMessageDialog(frame, "Could not read file.");
}
try { //closes file
frFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return record;
}
}
公共类主{
公共静态void main(字符串[]args){
final Guid Main=new Guid();//创建Guid的实例
Main.mergeButton.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0)
{
合并();
}
});
}
静态void merge()
{
//读取文件并转换为字符串
字符串pathone=open(Guid.pathone);
系统输出打印(pathone);
//解析和格式化
//字符串[]oneArray=pathone.replace(“\n”,”).split(“”);
//获取路径二文本
//字符串pathtwo=open(Guid.pathtwo);
//解析和格式化
//加载到数组中
//比较数组项
//添加新数组项
//排序数组
//将数组写入paththree文件
//对于(int i=0;i以这种方式执行。如果要读取整个文件,请删除reset()
和mark()
方法
StringBuilder record = new StringBuilder();
BufferedReader brFile = new BufferedReader(frFile);
String line = null;
while ((line = brFile.readLine()) != null) {
record.append(line).append("\n");
}
注:
- 别忘了关上小溪
- 使用finally block关闭流
- 使用StringBuilder或StringBuffer附加字符串
- 使用
System.getProperty(“line.separator”)
获取系统特定的行分隔符
查看readLine将阅读文档的第一行。
试用(未测试):
Niko您可能想使用作为初学者,我们需要堆栈跟踪。
String lineReaded;
while ((lineReaded=brFile.readLine())!=null)
{
record +=linereaded+"\n";
}