做一个BufferedReader出来,用readLine()方法读取就可以了。

解决方案 »

  1.   

    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("C:/a.txt")));for(String line = br.readLine(); line != null; line = br.readLine()) {
      System.out.println(line);
    }
    br.close();
      

  2.   

    现在,我只知道某个目录下有txt文件,文件名是未知的,我怎么去逐一读取该目录下的很多txt文件?
      

  3.   

    FileReader fr = new FileReader(str);//    文件名
        BufferedReader bufferedreader = new BufferedReader(fr);
        String instring;
        while ( (instring = bufferedreader.readLine()) != null) {
          String sss = instring.replaceAll(" ","");
          if(sss.length() != 0)
          {
             System.out.println(instring);
          }
        }
        fr.close();
    这是逐行读一个文件的代码!
      

  4.   

    // Servlet imports
    import javax.servlet.ServletContextListener;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContext;
    // IO imports
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.util.StringTokenizer;
    public class InitializeProductList implements ServletContextListener {  public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        String catalogFileName = context.getInitParameter("catalogFileName");
        InputStream is = null;
        BufferedReader catReader = null;    try {
          is = context.getResourceAsStream(catalogFileName);
          catReader = new BufferedReader(new InputStreamReader(is));
          String productString;
          ProductList catalog = new ProductList();      // Parse the catalog file to construct the product list
          while ( (productString = catReader.readLine()) != null ) {
            StringTokenizer tokens = new StringTokenizer(productString, "|");
            String code = tokens.nextToken();
            String price = tokens.nextToken();
            String quantityStr = tokens.nextToken();
            int quantity = Integer.parseInt(quantityStr);
            String description = tokens.nextToken();
            Product p = new Product(code, price, quantity, description);
            catalog.addProduct(p);
          }      // Store the catalog as an application-scoped attribute
          context.setAttribute("catalog", catalog);      context.log("The ProductList has been initialized.");      // Log any exceptions
        } catch (Exception e) {
          context.log("Exception occured while parsing catalog file.", e);      // Clean up resources
        } finally {
          if ( is != null ) {
            try { is.close(); } catch (Exception e) {}
          }
          if ( catReader != null ) {
            try { catReader.close(); } catch (Exception e) {}
          }
        }
      }  public void contextDestroyed(ServletContextEvent sce) {
        // TODO: rewrite the catalog file
      }
    }
    这是sl314上的一个例子。
      

  5.   

    TO  ChDw(米):
    你的程序报错:"ReadFile.java": cannot resolve symbol: class BufferedReader in class sms.ReadFile at line 16, column 5
      

  6.   

    TO  ChDw(米):
    你的程序报错:"ReadFile.java": cannot resolve symbol: class BufferedReader in class sms.ReadFile at line 16, column 5
    #这是我copy你的程序后的写了一个测试:
    public class ReadFile {
      public ReadFile() {
      }
      public void readfile() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("E:/netMAC.txt")));
        for(String line = br.readLine(); line != null; line = br.readLine()) {
        System.out.println(line);
        }
        br.close();
      }  public static void main(String[] args) {
        ReadFile rf = new ReadFile();
        rf.readfile() ;
      }}
      

  7.   

    读取目录下的文件可以用File类的listFiles()方法
        File path = new File("c:\\windows\\");
        
        File[] fileName = path.listFiles();
        
        System.out.println(fileName.length);
        for(int i=0;i<fileName.length;i++)
        {
          System.out.println(fileName[i].getName());
        }
      

  8.   

    import java.io.*;  你当然需要import相应的包啊
      

  9.   

    利用listFiles()把目录下所有的文件列出,再读出每一个文件的内容;
      

  10.   

    http://blog.csdn.net/cqq/archive/2004/09/23/114062.aspx
      

  11.   

    用两个流接在一起  一个FileInputStream流,一个bufferReader流