BufferedReader in=new Buffered(new FileReader(args[0]));//args[0]是传入参数
String s,s1;
while((s=in.readLine()!=null)
{
    System.out.println(s);
    s=in.readLine();
}

解决方案 »

  1.   

    我这样写编译不过哈:
    class cw1a{
    public static void main(String[] args){
                BufferedReader in=new Buffered(new FileReader(args[0]));
                String s;
                while( ( s=in.readLine() )!=null)
                {
                     System.out.println(s);
                     s=in.readLine();
                }        }
    }
    BufferedReader in=new Buffered(new FileReader(args[0]));这一行有3个错误,都是格式不能解析为什么?
      

  2.   

    有没有引入java.io类?
    import java.io.*;
      

  3.   

    不好意思是我写错了
    BufferedReader in=new BufferedReader(new FileReader(args[0]));
    你试试,我现在在外面
    import java.io.*;
    class cw1a{
    public static void main(String[] args){
                BufferedReader in=new BufferedReader(new FileReader(args[0]));
                String s;
                while( ( s=in.readLine() )!=null)
                {
                     System.out.println(s);
                     s=in.readLine();
                }        }
    }
      

  4.   

    对于filename,好象有个getFileName()方法
    你去查查ducument,我记不清
      

  5.   

    还是有错,还是3个,在new FileReader(args[0]));,while( ( s=in.readLine() )!=null)
    ,s=in.readLine();为什么?
      

  6.   

    unreported exception java.io.IOException;must be caught or declare
      

  7.   

    倒了
    又是我错
    实在不好意思,没document我不行
    import java.io.*;
    class cw1a{
    public static void main(String[] args){
               
            try{
                BufferedReader in=new BufferedReader(new FileReader(args[0]));
                String s;
                while( ( s=in.readLine() )!=null)
                {
                     System.out.println(s);
                     s=in.readLine();
                }
               }
           catch(Exception e){}
               }
    }
      

  8.   

    还有,你自己为什么不去查查API,这些问题看API是能够自己解决的
    例如这句话unreported exception java.io.IOException;must be caught or declare
    就表明有异常没有处理,那就要去处理啊,这其实你应该会自己调试的
      

  9.   

    谢谢,但现在还有问题,为什么我的文件读出来不对呢?
    文件格式:
    title 
    1
    2
    3
    4
    但我读出来显示的是
    title 
    2
    4
      

  10.   

    对了,另外我想文件名是程序运行后输入的怎么做呢?都是基础的用c 我会,但我现在不能查java的资料,请再说一下。谢谢
    就像:
    要求输入文件名:
    Enter data file: 1.txt
    打印文件: 
    Title: First Data Set 
    14 
    12 
    37 
    62
      

  11.   

    import java.io.*;
    class cw1a{
    public static void main(String[] args){
               
            try{
                BufferedReader in=new BufferedReader(new FileReader(args[0]));
                String s,s1;
                while( ( s=in.readLine() )!=null)
                {     
                     s1+=(s+"\n");
                     
                     s=in.readLine();
                }
                System.out.println(s1);
               }
           catch(Exception e){}
               }
    }
      

  12.   

    我try了,不要s=in.readLine();
    就可以了谢谢,上面那个问题怎么解决?
      

  13.   

    执行上面文件
    javac cwla
    java csla 文件名
    文件必须和csla在同一个文件夹,不然你得输入完整路径
      

  14.   

    您没有理解我的意识,我是想像c的scanf一样输入文件名称。
      

  15.   

    这个我倒没想过
    那最好做个GUI界面
    让用户输入文件名
      

  16.   

    你有没有QQ,这样很痛苦,QQ上说
      

  17.   

    我写个你看看吧
    import java.awt.*;
    import javax.swing.*;
    import java.io.*;
    import java.awt.event.*; 
    class cw1a{
          public static void main(String[] args){
               
               MyFrame frame1=new MyFrame();
               frame1.setSize(300,200);
               frame1.setVisible(true);
               frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               String filename=new String();
              }
               class buttonListener implements ActionListener
              {
                 public void actionPerformed(ActionEvent e)
                 {
                       try{ 
                                   filename=txt.getText();
        BufferedReader in=new BufferedReader(new FileReader(filename));
                String s,s1;
                while( ( s=in.readLine() )!=null)
                {     
                     s1+=(s+"\n");
                     
                     s=in.readLine();
                }
                System.out.println(s1);
             }
           catch(Exception e){}
           }
        }  }
     class MyFrame extends JFrame
     {   
        JLabel lb=new JLabel("请输入你要打开的文件名:");
        JTextField txt=new JTextField(10);
        JButton b=new JButton("确定");
        public void init()
       {
         BorderLayout layout=new BorderLayout();
         Container cp=getContentPane();
         cp.setLayout(layout());
         cp.add(layout.NORTH,lb);
         cp.add(layout.CENTER,txt);
         
         b.addActionListener(new buttonListener());
                             
         cp.add(layout.SOUTH,b);}
     }
    }   
    我随手写的,在这里没办法试
    你试试吧
      

  18.   

    cp.setLayout(layout)
    不用加()了,我写错的,汗
      

  19.   

    import java.awt.*;
    import javax.swing.*;
    import java.io.*;
    import java.awt.event.*; 
    class cw1a{
          public static void main(String[] args){
               
               MyFrame frame1=new MyFrame();
               frame1.setSize(300,200);
               frame1.setVisible(true);
               frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               String filename=new String();
              }
     class MyFrame extends JFrame
     {   
        JLabel lb=new JLabel("请输入你要打开的文件名:");
        JTextField txt=new JTextField(10);
        JButton b=new JButton("确定");
        public void init()
       {
         BorderLayout layout=new BorderLayout();
         Container cp=getContentPane();
         cp.setLayout(layout);
         cp.add(layout.NORTH,lb);
         cp.add(layout.CENTER,txt);
         
         b.addActionListener(new buttonListener());
                             
         cp.add(layout.SOUTH,b);}
     }
      
         class buttonListener implements ActionListener
          {
                 public void actionPerformed(ActionEvent e)
                 {
                       try{ 
                                   filename=txt.getText();
        BufferedReader in=new BufferedReader(new FileReader(filename));
                String s,s1;
                while( ( s=in.readLine() )!=null)
                {     
                     s1+=(s+"\n");
                     
                     s=in.readLine();
                }
                System.out.println(s1);
             }
           catch(Exception e){}
           }
        }  } }