0

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?

3 Answers 3

1

For reading files I would suggest following this site: Read File With Java.

As for arguments, I'm not sure if you mean into the program itself (command line arguments) or are talking about more input with the scanner. If you are talking about command line arguments, I would follow the Oracle Docs.

Good luck.

Sign up to request clarification or add additional context in comments.

Comments

1

If you insist on using scanner, here is the code

// Read each line in the file
    while(txt_again.hasNext()) {
        // Read each line and display its value
    System.out.println("First line:    " + txt_again.nextLine());
        // String whole_txt = whole_txt + txt_again.nextLine(); if you want all the contents in one string.
}

Or you can assign it to a string txt_name and print that out.

http://www.functionx.com/java/Lesson23.htm

For adding arguments, add a String[] args as one of the parameters in your main function.

    public static void main(String[] args)

And access the arguments as args[0], args[1] etc

Comments

0

Google Guava has utility methods for reading an entire file into a String, also for reading a file into a list of lines, etc.

For example,

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import com.google.common.base.Charsets;
import com.google.common.io.Files;

class Scratch {

    public static void main(String[] args) throws IOException {
        final String filename = args[0];

        final File txt = new File(filename);

        System.out.println("Here's your file: " + filename);
        System.out.println(Files.toString(txt, Charsets.UTF_8));

        System.out.print("Type the filename again: ");

        final String fileAgain = new Scanner(System.in).nextLine();

        final File txt_again = new File(fileAgain);
        System.out.println(Files.toString(txt_again, Charsets.UTF_8));
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.