如何将默认构造函数输出存储在字符串中


public class demo2{
public demo2() throws FileNotFoundException {
Scanner scanner = new Scanner(new 
File("/home/madhu/Desktop/demo.txt"));
while (scanner.hasNextLine())
System.out.println(scanner.nextLine());
}
public static void main(String[] args) throws FileNotFoundException {
demo2 obj = new demo2();
}
}

新建demo2((输出为
Data12001000
data22012000
如何将新的demo2((值存储到字符串数组中,或者如何写入以获得这种格式的输出?我想存储为
st1=Data12001000
st2=Data22012000

您可以这样做。

扫描仪阅读器.class

public class ScannerReader {
private ArrayList<String> scannerInput;
public ScannerReader() {
scannerInput = new ArrayList<>();
readFileInput();
}
private void readFileInput() {
try {
Scanner scanner = new Scanner(new File("src\date.txt"));
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
scannerInput.add(scanner.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void printArrayData() {
for(String line : scannerInput) {
System.out.println(line);
}
}

}

Main.class

public class Main {
public static void main(String[] args) {
ScannerReader reader = new ScannerReader();
reader.printArrayData();
}
}

最新更新