java怎么读取txt文件 并且把内容保存在一个数组里
文件格式如下:
5 3
1 3 5
5 6 8
6 7 8

解决方案 »

  1.   

    try {
    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();
    } }
      

  2.   

    文件读取并打开,自己慢慢看吧
    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());

    }
    }
      

  3.   

    如果我把文件直接放在了src下  我路径写什么? 
      

  4.   

    如果我把文件直接放在了src下  我路径写什么? File f = new File(这里面填什么);
      

  5.   

    如果那样就写相对路径
    FileReader myfile=new FileReader("test.txt");
      

  6.   

    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)
      

  7.   

    用的eclipse开发工具的话,默认的当前路径应该是该项目下和src文件夹同级吧?  用eclipse的话,相对路径应该是:
    FileReader file = new FielReader("./src/test.txt");
      

  8.   

    怎么会把txt文件放在src文件夹下呢,我一直是放在src文件夹同级的目录下的