文件选择后,我们要把文件的内容读取出来,显示在界面上,这里不把文件的全部内容读取出来,只读取我们想要的部分
注意,有时文件内容没有打印出来,先找原因,可能方法调用不对
首先先写个方法:
/*
**读取TXT文件方法
*/
public String readFile(String filePath){
String res=null;
StringBuilder sb = new StringBuilder();
int n =0;
try {
File file= new File(filePath);
InputStreamReader br=new InputStreamReader(new FileInputStream(file), "UTF-8");
BufferedReader reader = new BufferedReader(br);
String str;
while((str=reader.readLine())!=null){
sb.append(str+"\n");
System.out.println(str);//这会把每一行的结果显示出来
textArea.append(str+"\n");//这段代码会报错,要是出现空指针就禁用掉
n+=1;//这段代码会报错,要是出现空指针就禁用掉
System.out.println(n);
}
res=sb.toString();
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return res;
}
然后,读取按钮的实现:
//按读取按钮实现功能
btn_Button_readFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//读取文件路径
String strpath = jTextField.getText();
System.out.println(strpath);
if(e.getSource()==btn_Button_readFile){
String res=readFile(strpath);//读取文本中全部路径,可能结果会是null
mTotalContent=res;//存入
mDestFile=new File(strpath);//获取文件内容,可能结果会是null
//显示到大输入框
if(res!=null){
int start=res.indexOf(prefixStart)+prefixStart.length();//从某处开始
int end=res.indexOf(prefixEnd);//到某处结束
String finalRes=res.substring(start, end);//大输入框只显示文件内某处开始到结束的范围
System.out.print(finalRes);
textArea.setText("");
textArea.append(finalRes+"\n");//显示内容到大输入框中
}
}
}
});
再来,最新项目的,用一样的方法不对,微调了下,自己对比下前面跟这个差别在哪儿
private JTextArea textArea;// 大输入框大滚动条,初始化,全局变量
前面设置全局变量后,后面就不用在设置了
// 大文本框
JTextArea textArea = new JTextArea();//前面已经初始化过了,这里在初始化就会报错,所以这行去掉红色字体
textArea.setBounds(1, 184, 263, 152);
layeredPane.add(textArea);
这里读取文件的方法没有什么大改动
/*
* *读取文件方法
*/
public String readFile(String filePath) {
System.out.println("readFile:" + filePath);
String res = null;
StringBuilder sb = new StringBuilder();
int n = 0;
try {
File file = new File(filePath);
InputStreamReader br = new InputStreamReader(new FileInputStream(
file), "UTF-8");
BufferedReader reader = new BufferedReader(br);
String str;
while ((str = reader.readLine()) != null) {
System.out.println("readFiledddd:" + filePath);
sb.append(str + "\n");
System.out.println("55:" + str);
/* textArea.setText("");
textArea.append(str+"\n");*/
n += 1;
System.out.println("44:" + n);
/*
* //显示到大输入框 if(res!=null){ textArea.setText("");
* textArea.append(str+"\n");//显示内容到大输入框中 }
*/
}
res = sb.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return res;
}
读取按钮在文件框显示
/* final String prefixStart = "[DUT1 Cable Loss Mapping]";// 从[DUT1 Cable Loss Mapping]开始
final String prefixEnd = "[DUT1 Tas Cable Loss Mapping]";// 到[DUT1 TasCable Loss Mapping]结束
*/
// 按读取按钮实现功能
btnNewButton_read.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// 读取文件路径
String strpath = textField_choose.getText();
System.out.println("strpath22" + strpath);
// if(e.getSource()==btnNewButton_read){
String res = readFile(strpath);// 读取文本中全部路径
System.out.println("res:" + res);
textArea.setText(res);//把内容显示到大显示框中,这里就一句话
mTotalContent = res;// 存入
System.out.println("bb:" + mDestFile);
mDestFile = new File(strpath);// 获取文件内容
System.out.println("33:" + mDestFile);
}
});