当前位置: 技术问答>java相关
大家能否帮我解决一个关于读文件的问题,谢谢!
来源: 互联网 发布时间:2015-05-09
本文导语: 我在写一个关于读文件的程序,我用了如下的写法: void openfile(String filename){ try{ File file=new File(filename); int size=(int)file.length(); int chars_read=0; char[] data=new char[size]; BufferedReader br=new BufferedR...
我在写一个关于读文件的程序,我用了如下的写法:
void openfile(String filename){
try{
File file=new File(filename);
int size=(int)file.length();
int chars_read=0;
char[] data=new char[size];
BufferedReader br=new BufferedReader(new FileReader(file));
while(br.ready()){
chars_read=chars_read+br.read(data,chars_read,size-chars_read);
}
br.close();
jTextPane1.setText(new String(data,0,chars_read));
statusBar.setText("Opened"+filename);
}
catch(IOException e){
statusBar.setText("Error opening"+filename);
}
听说可以用别的方法也可以,不知道哪位高手可以帮我再另写一个,功能比这个还要强大一些的程序,在这里我先谢谢了!
void openfile(String filename){
try{
File file=new File(filename);
int size=(int)file.length();
int chars_read=0;
char[] data=new char[size];
BufferedReader br=new BufferedReader(new FileReader(file));
while(br.ready()){
chars_read=chars_read+br.read(data,chars_read,size-chars_read);
}
br.close();
jTextPane1.setText(new String(data,0,chars_read));
statusBar.setText("Opened"+filename);
}
catch(IOException e){
statusBar.setText("Error opening"+filename);
}
听说可以用别的方法也可以,不知道哪位高手可以帮我再另写一个,功能比这个还要强大一些的程序,在这里我先谢谢了!
|
俺来补充一下........
public boolean fileRead(String filename){
BufferedReader in;
String sLine= "";
JTextArea jTextArea = new JTextArea();
try {
in = new BufferedReader( new FileReader(filename) );
}catch(IOException e) {
JOptionPane.showMessageDialog(this, "Read ERR", "Err", JOptionPane.ERROR_MESSAGE);
return false;
}
try{
while ((sLine = in.readLine()) != null){
jTextArea.append(sLine + "/n");
}
}catch(Exception e){
return false;
}
}
public boolean fileRead(String filename){
BufferedReader in;
String sLine= "";
JTextArea jTextArea = new JTextArea();
try {
in = new BufferedReader( new FileReader(filename) );
}catch(IOException e) {
JOptionPane.showMessageDialog(this, "Read ERR", "Err", JOptionPane.ERROR_MESSAGE);
return false;
}
try{
while ((sLine = in.readLine()) != null){
jTextArea.append(sLine + "/n");
}
}catch(Exception e){
return false;
}
}