Sto provando a leggere da che cosa è sulle prime due righe di un file txt, metterlo su una stringa e passare quella stringa sui miei metodi. Sono confuso su ciò che devo fare nel metodo principale.Come leggere in un file txt
Heres quello che ho:
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);
}
ho provato String x = new String (x.getNewFile()) non sono sicuro se questo è il modo giusto per andare con quella o no.
Devo provare a prendere forse? – TMan
puoi circondare con try/catch block per catturare l'eccezione I/O sicura. O il tuo metodo potrebbe avere un lancio per l'eccezione. – Justin
Non ne trovo nessuno quando lo eseguo. Per testarlo ho passato line1 in un metodo clas.test (line1) ... Non ottenendo alcun tipo di output – TMan