I am currently learning Java for an intro CS class. I have beginner experience with Python, which I learned from the eTextbook Learn Python the Hard Way (http://learnpythonthehardway.org/book/).
I am learning Java by converting the Python source in the book to Java source.
I am stuck on opening and reading files in Java. I want to convert this Python code (exercise 15 in the book) to Java:
from sys import argv
script, filename = argv
txt = open(filename)
print "Here's your file %r:" % filename
print txt.read()
print "Type the filename again:"
file_again = raw_input("> ")
txt_again = open(file_again)
print txt_again.read()
This is what I have for opening the file in Java:
System.out.println("Type the filename again: ");
Scanner in = new Scanner(System.in);
String file_name = in.nextLine();
try
{
Scanner txt_again = new Scanner(new File(file_again));
}
catch ( IOException e)
{
System.out.println("Sorry but I was unable to open your file");
e.printStackTrace();
System.exit(0);
}
How do I output the opened file (a text file)? Also, how do I add arguments (i.e. script, filename) in Java?