Java读取文件到TextArea的实现

//文件打开函数
	private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:
		//初始化文件选择窗口
		FileDialog fileDialog;
		//An abstract representation of file and directory pathnames. 
		File file = null;		
		Frame frame = null;		
		fileDialog = new FileDialog(frame, "Open", FileDialog.LOAD);
		fileDialog.setVisible(true); //创建并显示打开文件对话框

		try { //以缓冲区方式读取文件内容
			
	        jTextArea1.setText("");
			file = new File(fileDialog.getDirectory(), fileDialog.getFile());
			FileReader filereader = new FileReader(file);
			BufferedReader bufferreader = new BufferedReader(filereader);
			String aline;
			while ((aline = bufferreader.readLine()) != null)
				//按行读取文本,显示在TEXTAREA中
				jTextArea1.append(aline + "\r\n");
			filereader.close();
			bufferreader.close();
		
		} catch (IOException e) {
			System.out.println(e);
		}
	}

你可能感兴趣的:(Java)