Input and Output in Java

User input in Java:

import java.util.Scanner;

public class Main{

public static void main(String[] args){

Scanner userInput = new Scanner(System.in);

System.out.printf(“Enter your favorite artist: “);

String favArtist = userInput.nextLine();

System.out.println(“You like ” + favArtist + “!”);

}

}

Check if file exists in Java:

You will need to import java.io.*; and then put this code into your main method:

File someFile = new File(“example.txt”);

System.out.println(someFile.exists());

Reading from a file in Java:

Save a file called example.txt with the following text:

here is line 1

another line

line three

woohoo

ok

Then use this code, and enter example.txt when prompted for user input:

import java.io.*;

import java.util.Scanner;

public class Main{

public static void main(String[] args) throws FileNotFoundException{

Scanner fileIn;

Scanner userInput = new Scanner(System.in);

System.out.println(“Enter the filename: “);

String filename = userInput.next();

File someFile = new File(filename);

if (someFile.exists()){

System.out.println(“file exists”);

fileIn = new Scanner(someFile);

System.out.println(“Contents of file: “);

while (fileIn.hasNext()) {

System.out.println(fileIn.nextLine());

}

} else {

System.out.println(“no file with that filename/path”);

}

}

}

Please keep in mind that long lines of code in this book get wrapped around to the next line. If you have an error following this code, it could be that you’re putting in an extra line break somewhere.

Create a new file in Java:

import java.io.*;

public class Main{

public static void main(String[] args) throws IOException{

System.out.println(“Creating new file example123.txt”);

File someFile = new File(“example123.txt”);

if (!someFile.exists()){

System.out.println(“file does not already exist”);

System.out.println(“safe to make new file”);

File exampleFile = new File(“example123.txt”);

try {

exampleFile.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

} else {

System.out.println(“error: file already exists”);

System.out.println(“not going to overwrite”);

}

}

}

Deleting a file in Java:

import java.io.*;

public class Main{

public static void main(String[] args) throws IOException{

System.out.println(“deleting file example123.txt”);

File someFile = new File(“example123.txt”);

if (someFile.exists()){

someFile.delete();

System.out.println(“file was deleted”);

} else {

System.out.println(“no such file to delete”);

}

}

}

Java writing over a file:

import java.io.*;

public class Main{

public static void main(String[] args) throws IOException{

System.out.println(“overwriting the contents of example.txt”);

File someFile = new File(“example.txt”);

PrintWriter fileOut = new PrintWriter(someFile);

fileOut.write(“everything else in the file is gone now”);

fileOut.close();

}

}

Java appending to a file:

import java.io.*;

public class Main{

public static void main(String[] args) throws IOException{

System.out.println(“overwriting the contents of example.txt”);

File someFile = new File(“example.txt”);

FileWriter myFileWriter = new FileWriter(someFile, true);

//the second boolean argument means appending mode is true

PrintWriter fileOut = new PrintWriter(myFileWriter);

fileOut.write(“\nthis is being appended”);

fileOut.close();

}

}

← Previous | Next →

Java Topic List

Main Topic List

Leave a Reply

Your email address will not be published. Required fields are marked *