我正在尝试从txt文件的前两行中的内容中读取内容,将其放在字符串上,然后将该字符串传递到我的方法中。我对主要方法中需要做的事情感到困惑。

这是我所拥有的:

    public class TestingClass  {
Stacked s;// = new Stacked(100);
 String Finame;

public TestingClass(Stacked stack) {
    //Stacked s = new Stacked(100);
    s = stack;
    getFileName();
    readFileContents();
}

public void readFileContents() {
    boolean looping;
    DataInputStream in;
    String line = "" ;
    int j, len;
    char ch;

    try {
        in = new DataInputStream(new FileInputStream(Finame));
        len = line.length();
        for(j = 0; j<len; j++) {
                System.out.println("line["+j+"] = "+line.charAt(j));
        }
    }

    catch(IOException e) {
            System.out.println("error " + e);
    }
}

public void getFileName() {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter File Name");
    Finame = in.nextLine();
    System.out.println("You Entered " + Finame);
}

       public static void main(String[] args) {
         Stacked st = new Stacked(100);
         TestingClass clas = new TestingClass(st);
         //String y = new String("(z * j)/(b * 8) ^2");
         // clas.test(y);

       }

我试过了String x = new String(x.getNewFile())我不确定这是否是正确的方法。

最佳答案

试试这个:

File file = new File("file.txt");

BufferedReader reader = new BufferedReader(new FileReader(file));
String line1 = reader.readLine();
String line2 = reader.readLine();

09-11 19:26
查看更多