java.io.FileNotFoundException: test.txt (系统找不到指定的文件。) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:106) at java.io.FileReader.<init>(FileReader.java:55) at com.yao.Test.ReadFile(Test.java:15) at com.yao.Test.main(Test.java:36) Exception in thread "main" java.lang.NullPointerException at com.yao.Test.main(Test.java:37)
用的eclipse开发工具的话,默认的当前路径应该是该项目下和src文件夹同级吧? 用eclipse的话,相对路径应该是: FileReader file = new FielReader("./src/test.txt");
List l = new ArrayList<String>();
File file = new File("d:\\a.txt");
FileReader fr = new FileReader(file);
LineNumberReader lnr = new LineNumberReader(fr);
String line = lnr.readLine();
while (line != null) {
String[] nums = line.split("\\s+");
line = lnr.readLine();
for (String num : nums) {
l.add(num);
System.out.println(num);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
import java.awt.*;
import java.awt.event.*;
import java.io.FileReader;import javax.swing.*;public class openfile extends JFrame{
JButton b1=new JButton("选择文件");
JTextField t1=new JTextField(20);
JTextArea result=new JTextArea();
JButton b2=new JButton("打开");
FileReader myfile;
Font zi=new Font("宋体",Font.TYPE1_FONT,15);
public openfile() {
setTitle("文件打开演示");
setLayout(null);
setSize(700, 600);
setResizable(false);
this.setBackground(Color.DARK_GRAY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = this.getSize();
setLocation( (scr.width - frm.width) / 2,
(scr.height - frm.height) / 2 );
b1.setBounds(50, 30, 100, 25);
t1.setBounds(180,30 , 180, 25);
JScrollPane s1 = new JScrollPane(result);
result.setBackground(Color.lightGray);
result.setFont(zi);
s1.setBounds(20, 100, 650, 450);
b2.setBounds(400, 30, 80, 25);
add(b1);
add(t1);
add(s1);
add(b2);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FileDialog my=new FileDialog(openfile.this);
my.setVisible(true);
t1.setText(my.getDirectory()+my.getFile());
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
myfile=new FileReader(t1.getText());
int show=myfile.read();
while(show!=-1){
result.append((char)show+"");
show=myfile.read();
}
}
catch(Exception ee){}
}
});
setVisible(true);
}
public static void main(String[] args){
new openfile();
System.out.print(new java.util.Date());
}
}
FileReader myfile=new FileReader("test.txt");
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileReader.<init>(FileReader.java:55)
at com.yao.Test.ReadFile(Test.java:15)
at com.yao.Test.main(Test.java:36)
Exception in thread "main" java.lang.NullPointerException
at com.yao.Test.main(Test.java:37)
FileReader file = new FielReader("./src/test.txt");