一道java问题,如何实现文件内容的读取
我想设计一个程序,该程序可以打开一个文件,然后把文件的内容显示在一个文本框内,但是在文件内容的读取和写出这部分出了问题,编译不通过。请高手帮忙看一下程序出错在哪里,并且修...
我想设计一个程序,该程序可以打开一个文件,然后把文件的内容显示在一个文本框内,但是在文件内容的读取和写出这部分出了问题,编译不通过。请高手帮忙看一下程序出错在哪里,并且修改一下,谢谢
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class Test {
JFrame frame=new JFrame("读取文件");
JTextArea ta=new JTextArea();
public Test() {
JButton b1=new JButton("open");
b1.addActionListener(new al());
Container cp=frame.getContentPane();
cp.setLayout(new BorderLayout());
cp.add(b1,BorderLayout.NORTH);
cp.add(ta,BorderLayout.CENTER);
frame.setSize(500,500);
frame.setVisible(true);
}
class al implements ActionListener{
public void actionPerformed(ActionEvent e){
JFileChooser fc=new JFileChooser();
fc.showOpenDialog(null);
int value=0;
if(value==JFileChooser.APPROVE_OPTION){
String filename=null;
String filedirectory=null;
String file_and_directoryname=null;
filename=fc.getSelectedFile().getName();
filedirectory=fc.getCurrentDirectory().toString();
file_and_directoryname=filedirectory+"//"+filename;
FileReader filein=new FileReader(file_and_directoryname);
FileOutputStream fileout=new FileOutputStream(String str);
int c=0;
while((c=filein.read()) != -1) {
fileout.write(c);
}
ta.setText("文件内容是:"+str);
filein.close();
fileout.close();
}
}
}
public static void main(String []args){
new Test();
}
} 展开
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class Test {
JFrame frame=new JFrame("读取文件");
JTextArea ta=new JTextArea();
public Test() {
JButton b1=new JButton("open");
b1.addActionListener(new al());
Container cp=frame.getContentPane();
cp.setLayout(new BorderLayout());
cp.add(b1,BorderLayout.NORTH);
cp.add(ta,BorderLayout.CENTER);
frame.setSize(500,500);
frame.setVisible(true);
}
class al implements ActionListener{
public void actionPerformed(ActionEvent e){
JFileChooser fc=new JFileChooser();
fc.showOpenDialog(null);
int value=0;
if(value==JFileChooser.APPROVE_OPTION){
String filename=null;
String filedirectory=null;
String file_and_directoryname=null;
filename=fc.getSelectedFile().getName();
filedirectory=fc.getCurrentDirectory().toString();
file_and_directoryname=filedirectory+"//"+filename;
FileReader filein=new FileReader(file_and_directoryname);
FileOutputStream fileout=new FileOutputStream(String str);
int c=0;
while((c=filein.read()) != -1) {
fileout.write(c);
}
ta.setText("文件内容是:"+str);
filein.close();
fileout.close();
}
}
}
public static void main(String []args){
new Test();
}
} 展开
1个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class Test {
JFrame frame = new JFrame("读取文件");
JTextArea ta = new JTextArea();
public Test() {
JButton b1 = new JButton("open");
b1.addActionListener(new al());
Container cp = frame.getContentPane();
cp.setLayout(new BorderLayout());
cp.add(b1, BorderLayout.NORTH);
cp.add(ta, BorderLayout.CENTER);
frame.setSize(500, 500);
ta.setLineWrap(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class al implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
int value = fc.showOpenDialog(null);
if (value == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
FileReader fr;
try {
fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while (line != null) {
ta.append(line);
line = br.readLine();
}
br.close();
fr.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
// String filename=null;
// String filedirectory=null;
// String file_and_directoryname=null;
// filename=fc.getSelectedFile().getName();
// filedirectory=fc.getCurrentDirectory().toString();
// file_and_directoryname=filedirectory+"//"+filename;
// FileReader filein=new FileReader(file_and_directoryname);
// FileOutputStream fileout=new FileOutputStream(filein);
// int c=0;
// while((c=filein.read()) != -1) {
// fileout.write(c);
// }
// ta.setText("文件内容是:"+str);
// filein.close();
// fileout.close();
}
}
}
public static void main(String[] args) {
new Test();
}
}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class Test {
JFrame frame = new JFrame("读取文件");
JTextArea ta = new JTextArea();
public Test() {
JButton b1 = new JButton("open");
b1.addActionListener(new al());
Container cp = frame.getContentPane();
cp.setLayout(new BorderLayout());
cp.add(b1, BorderLayout.NORTH);
cp.add(ta, BorderLayout.CENTER);
frame.setSize(500, 500);
ta.setLineWrap(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class al implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
int value = fc.showOpenDialog(null);
if (value == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
FileReader fr;
try {
fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while (line != null) {
ta.append(line);
line = br.readLine();
}
br.close();
fr.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
// String filename=null;
// String filedirectory=null;
// String file_and_directoryname=null;
// filename=fc.getSelectedFile().getName();
// filedirectory=fc.getCurrentDirectory().toString();
// file_and_directoryname=filedirectory+"//"+filename;
// FileReader filein=new FileReader(file_and_directoryname);
// FileOutputStream fileout=new FileOutputStream(filein);
// int c=0;
// while((c=filein.read()) != -1) {
// fileout.write(c);
// }
// ta.setText("文件内容是:"+str);
// filein.close();
// fileout.close();
}
}
}
public static void main(String[] args) {
new Test();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询