Here is some sample code...I'd like to be able to test the code out by opening a file with some lines in it. But I don't know how to do this, can anybody please help me? I'm using JCreator. Thanks.
package samplecode;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Part0 {
/**
* Read lines one at a time from r. After reading all lines, output
* all lines to w, outputting duplicate lines only once. Note: the order
* of the output is unspecified and may have nothing to do with the order
* that lines appear in r.
* @param r
* @param w
* @throws IOException
*/
public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
Set<String> s = new HashSet<String>();
String line;
int n = 0;
while ((line = r.readLine()) != null) {
s.add(line);
n++;
}
Iterator<String> i = s.iterator();
while (i.hasNext()) {
w.println(i.next());
}
}
/**
* The driver. Open a BufferedReader and a PrintWriter, either from System.in
* and System.out or from filenames specified on the command line, then call doIt.
* @param args
*/
public static void main(String[] args) {
try {
BufferedReader r;
PrintWriter w;
if (args.length == 0) {
r = new BufferedReader(new InputStreamReader(System.in));
w = new PrintWriter(System.out);
} else if (args.length == 1) {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(System.out);
} else {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(new FileWriter(args[1]));
}
long start = System.nanoTime();
doIt(r, w);
w.flush();
long stop = System.nanoTime();
System.out.println("Execution time: " + 10e-9 * (stop-start));
} catch (IOException e) {
System.err.println(e);
System.exit(-1);
}
}
}
Thanks.
How do I open a file with this code?
Started by heat89, Sep 22 2010 05:11 PM
5 replies to this topic
#1
Posted 22 September 2010 - 05:11 PM
|
|
|
#2
Posted 23 September 2010 - 12:47 AM
Basically, you'd give the name of the input and output files when you call the method from the command line; those if (args.length) statements in there are determining whether you specified a file or not. If you specift two files, e.g.
So basically, you need to use the command line to call the program, and specify the input and output files as arguments, as in the code above.
Hope this helps :)
java Part0 file1.txt, file2.txt, then file1 will be used as input, and file2 as output. If there are no files, it will default to System.in for the input (whatever you type at the command line) and use System.out for the output (so, it will print everything back to the command line). The oprogram continues until an empty line is reached. If you only specify one file, it will read the text from that file, and output it to the command line.
So basically, you need to use the command line to call the program, and specify the input and output files as arguments, as in the code above.
Hope this helps :)
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)
#3
Posted 23 September 2010 - 05:47 AM
Hey Fae...thanks alot for your reply.
Unfortunately though when I try that it does nothing. I've tried specifying an input file and output file, just an input file, and nothing works, it just sits there.
Can you think of any reason for this? The input and output text files are both in the same folder as the Part0.java file.
Thanks.
Unfortunately though when I try that it does nothing. I've tried specifying an input file and output file, just an input file, and nothing works, it just sits there.
Can you think of any reason for this? The input and output text files are both in the same folder as the Part0.java file.
Thanks.
#4
Posted 23 September 2010 - 06:02 AM
Worked fine for me...

Did you remember to put the file extention in too (e.g. I had input.txt and output.txt, not just input and output)
Also, I'm not too familiar with JCreator... that is to say, I've never even heard of it before your post ^^ When it compiles your .java files, where does it put the .class file? The .java file is before compilation (the source code), but the .class file is the compiled code that actually runs. Your files would have to be in the same directory as the .class file to work.

Did you remember to put the file extention in too (e.g. I had input.txt and output.txt, not just input and output)
Also, I'm not too familiar with JCreator... that is to say, I've never even heard of it before your post ^^ When it compiles your .java files, where does it put the .class file? The .java file is before compilation (the source code), but the .class file is the compiled code that actually runs. Your files would have to be in the same directory as the .class file to work.
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)
#5
Posted 23 September 2010 - 06:28 AM
What are you using? Eclipse?
#6
Posted 23 September 2010 - 06:32 AM
Notepad++, basically the same as GEdit, it's just a text editor that highlights words relevant to whichever language you're using, matches up {}'s etc...
I compile (and run, when necessary) with with Powershell.
:)
I compile (and run, when necessary) with with Powershell.
:)
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)


Sign In
Create Account

Back to top









