欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

bufferReader读取文本 换行后的内容读取不出

程序员文章站 2022-07-12 13:22:30
...

bufferReader读取文本 换行后的内容读取不出

		File file = new File("d:\\test.txt");
		BufferedReader bufferedReader;
		String text  = "";
		try {
			bufferedReader = new BufferedReader(new FileReader(file));
			String line = null;
			line = bufferedReader.readLine();
			System.out.println("------------- "+line);
			while(line != null){
				text += line.trim() + "\n";
				System.out.println("notNull");
			}
			System.out.println("text -------------- "+text);
			bufferedReader.close();
			bufferedReader = null;
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "错误提示", JOptionPane.ERROR_MESSAGE);//弹出窗口显示错误原因
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

 后来将while的判断条件改为

line = bufferedReader.readLine();
			System.out.println("------------- "+line);
			while((line = bufferedReader.readLine())!= null){
				text += line.trim() + "\n";
				System.out.println("notNull");
			}

 即可