Creating and writing to a text file using Java
Автор: David Wills
Загружено: 2021-05-12
Просмотров: 864
Here is the Java code:
import java.io.FileWriter; // import the FileWriter class
import java.io.File; // import the File class
import java.util.Scanner; // import Scanner class to read text files
public class WriteFile {
public static void main(String[] args) throws Exception {
Scanner scan;
FileWriter file;
String data, content;
int num;
File myFile;
scan = new Scanner(System.in); // Create a Scanner object
file = new FileWriter("students.txt"); // Create a FileWriter object with "students.txt" as parameter
System.out.println("Please enter your name: ");
data = scan.nextLine();
file.write("Name is: " + data);
file.write("\n");
System.out.println("Please enter your age in years(integer): ");
num = scan.nextInt();
file.write("Age is: " + num);
file.close();
// Show contents of file created
myFile = new File("students.txt");
scan = new Scanner(myFile); // Create a Scanner object from the file object above
while (scan.hasNextLine())
{
content = scan.nextLine();
System.out.println(content);
}
scan.close();
}
}
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: