×

Loading...
Ad by
  • 技多不压身,工到自然成:安省技工证书特训班,点击咨询报名!
Ad by
  • 技多不压身,工到自然成:安省技工证书特训班,点击咨询报名!

eg.

import java.io.File;
public static void main(String[] args)
{
FileControl fFiles = new FileControl();
while()
{
String sLines = fFiles.GetValue();
System.out.println(sLines);
}
}

public class FileControl()
{
File yourTestFile;
public FileControl()
{
/* open your testing file
yourTestFile = new File(/*your file's name*/);
}
protected String GetValue()
{
/* read one line */
return yourTestFile.readLine();
}
protected void close()
{
try{
yourTestFile.close();
}catch(Excepiton a){}
}
}
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 初手相问一个关于java的问题。我需要读取一个外部文件。有两个问题: 1。怎样实现一行行的读?我需要一次读一行,下次读下一行。 2。每行有几列数据,由空格分开。每次读一行时怎样只读去有用的数据?急等,谢谢
    • BufferedReader in = new BufferedReader(new FileReader("foo.in")); String strLine = in.readLine();
      • 谢谢谢.现在的问题是怎样将字符串形式存储的转换成数字呢?比如说以字符串形式存的0.123怎样转成数字0.123?
        • Double.parseDoublic(str);
        • BigDecimal(str) for scale manipulationi
    • 有最后一个问题.我要读取的文件的数据有几万行.我需要一次读一行,然后用这行的数据进行一系列的操作,然后读取第二行的再进行这些操作.我编写了一个class专门读文件,另一个class进行其他操作.readLine()这个方法可以读取一行,问题是
      当我在另一个class中完成那些操作再来读的时候,readLine()读的仍然是那行,怎样读取下一行呢?谢谢
      • donot close then reopen the file.
        • 我在读文件的class时的确没有关,没有in.close();就是不行呀。继续读下一行,是不是就是继续调用读文件的方法?我的程序贴在正文中,请帮忙看看。急,谢谢。
          /*
          * ReadFileTest.java
          *
          * Created on 2003年11月18日, 下午2:52
          */

          /**
          *
          * @author hua
          */
          public class ReadFileTest {
          ReadFile readFile=new ReadFile();


          /** Creates a new instance of ReadFileTest */
          public ReadFileTest() {
          readFile.GetValue(); //read a line in the file
          /* ReadFile is the class defined to read the file. GetValue is the method to get the useful values of the current line.*/

          System.out.println("1.arrival time: "+readFile.cust.arrivalTime);
          System.out.println("1.length: "+readFile.cust.length);

          readFile.GetValue(); // read another line
          System.out.println("1.arrival time: "+readFile.cust.arrivalTime);
          System.out.println("1.length: "+readFile.cust.length);
          }


          public static void main(String[] args) {new ReadFileTest();

          }

          }

          运行后,打印出来的数值是一样的。没有读下一行。什么问题呢?
          • what are the source codes of class ReadFile?
          • eg.
            import java.io.File;
            public static void main(String[] args)
            {
            FileControl fFiles = new FileControl();
            while()
            {
            String sLines = fFiles.GetValue();
            System.out.println(sLines);
            }
            }

            public class FileControl()
            {
            File yourTestFile;
            public FileControl()
            {
            /* open your testing file
            yourTestFile = new File(/*your file's name*/);
            }
            protected String GetValue()
            {
            /* read one line */
            return yourTestFile.readLine();
            }
            protected void close()
            {
            try{
            yourTestFile.close();
            }catch(Excepiton a){}
            }
            }
    • 谢谢各位.你们比国内收了我的钱的网站回答的还快还准确.这个小程序试成了.但有个疑问,这个疑问对以后程序有很大的影响,希望你们能几许帮我看看.我的程序在正文中.
      本文发表在 rolia.net 枫下论坛以下是ReadFile类.

      import java.io.*;
      import java.lang.*;
      public class ReadFile {

      int i=1;
      public Customer cust;
      String strLine;

      /** Creates a new instance of ReadFileTest */
      public ReadFile() {
      try{
      cust=new Customer();
      BufferedReader in=new BufferedReader(new FileReader("/home/hua/a-work/project-simu/bc2"));
      while ((strLine=in.readLine())!=null){

      //get the arrival time
      String str1=strLine.substring(5, 13);
      str1=str1.trim();
      Double num1=Double.valueOf(str1);
      cust.arrivalTime=num1.doubleValue();

      //get the cust length

      String str2=strLine.substring(13, strLine.length());
      str2=str2.trim();
      Double num2=Double.valueOf(str2);
      cust.length=num2.doubleValue();

      System.out.println(i+".arrival time: "+cust.arrivalTime);
      System.out.println(i+".length: "+cust.length);
      i++;

      // a small example try to tackle the data in other class
      ReadFileTest.DoSomething(cust);

      }

      }
      catch (IOException e){
      e.printStackTrace();
      }
      }
      }

      下面是ReadFileTest类.

      public class ReadFileTest {


      /** Creates a new instance of ReadFileTest */
      public ReadFileTest() {
      }

      /**
      * @param args the command line arguments
      */
      public static void DoSomething(Customer cust){
      cust.arrivalTime=cust.arrivalTime+100;
      System.out.println(".arrival time: "+cust.arrivalTime);
      System.out.println(".length: "+cust.length);
      }


      public static void main(String[] args) {
      new ReadFile();
      }
      }

      问题是,ReadFileTest中的DoSomething方法必需是static,否则会报错:
      ReadFile.java [44:1] non-static method DoSomething(Customer) cannot be referenced from a static context
      ReadFileTest.DoSomething(cust);
      ^
      为什么ReadFileTest.DoSomething(cust)是static context呢?这里的DoSomething只是个小例子,真正以后要作的操作是很复杂的而且不太肯可能一定是static的方法.怎么修正,让它不要求非是static的方法? 谢谢更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • bacause this line ReadFileTest.DoSomething(cust);
        • 不明白.能说详细点吗?
          • 那一行程序中你调用了ReadFileTest类的一个方法而没有创建它的实例,所以这个方法只能是静态的。
            • 你们说的很对,我创建实例调用就可以了.刚接触这些东西,基本概念很不熟呀.呵呵