writing to a text file java
PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
writer.println("The first line");
writer.println("The second line");
writer.close();
writing to a text file java
PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
writer.println("The first line");
writer.println("The second line");
writer.close();
java write to text file
import java.io.FileWriter; // Import the FileWriter class
import java.io.IOException; // Import the IOException class to handle errors
public class WriteToFile2 {
public static void main(String[] args) {
try {
FileWriter myWriter = new FileWriter("filename.txt");
myWriter.write("Files in Java might be tricky, but it is fun enough!");
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
java how to read a text file
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
class Scratch{
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("filename"));
input.next(); //returns next String
input.nextLine(); //returns next Line
input.nextBoolean();//returns next Boolean
input.nextInt(); //returns next Int
input.nextDouble(); //returns next double
...
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us