The Artima Developer Community
Sponsored Link

Java Answers Forum
Working with text files

4 replies on 1 page. Most recent reply: Apr 21, 2003 2:09 AM by Oliver

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Vitali

Posts: 1
Nickname: vitalip
Registered: Apr, 2003

Working with text files Posted: Apr 19, 2003 8:11 AM
Reply to this message Reply
Advertisement
Hello!

I'm beginner in Java and I'd like to start my study with file manipulations.Please help:
I need to read some text file(input file), to do something with it and write content of that file to other(output file).How I can do it?
PS! Filenames must be given by command line: e.g

C:\java nameOFprogram inputfile > outputfile

It's possible in Java?

Thank You very much


Oliver

Posts: 12
Nickname: lazycat
Registered: Dec, 2002

Re: Working with text files Posted: Apr 21, 2003 2:04 AM
Reply to this message Reply

/* InOut.java */
import java.io.*;

public class InOut {
public static void main(String [] args){

if(args.length != 2){
System.out.println("error:usage IoOut infile outfile");
System.exit(1);
}
try{
BufferedReader in = new BufferedReader(
new FileReader(args[0]));
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(args[1])));

String line;
while( (line = in.readLine()) != null){
//do something with input
//below is some method of String Object
//matches(String regex)
//replaceFirst(String regex, String replacement)
//replaceAll(String regex, String replacement)

//output
out.println(line);
}
in.close();
out.close();
}
catch(Exception ex){
ex.printStackTrace();
}
}
}

Oliver

Posts: 12
Nickname: lazycat
Registered: Dec, 2002

Re: Working with text files Posted: Apr 21, 2003 2:04 AM
Reply to this message Reply

/* InOut.java */
import java.io.*;

public class InOut {
public static void main(String [] args){

if(args.length != 2){
System.out.println("error:usage IoOut infile outfile");
System.exit(1);
}
try{
BufferedReader in = new BufferedReader(
new FileReader(args[0]));
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(args[1])));

String line;
while( (line = in.readLine()) != null){
//do something with input
//below is some method of String Object
//matches(String regex)
//replaceFirst(String regex, String replacement)
//replaceAll(String regex, String replacement)

//output
out.println(line);
}
in.close();
out.close();
}
catch(Exception ex){
ex.printStackTrace();
}
}
}

Oliver

Posts: 12
Nickname: lazycat
Registered: Dec, 2002

Re: Working with text files Posted: Apr 21, 2003 2:05 AM
Reply to this message Reply

/* InOut.java */
import java.io.*;

public class InOut {
public static void main(String [] args){

if(args.length != 2){
System.out.println("error:usage IoOut infile outfile");
System.exit(1);
}
try{
BufferedReader in = new BufferedReader(
new FileReader(args[0]));
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(args[1])));

String line;
while( (line = in.readLine()) != null){
//do something with input
//below is some method of String Object
//matches(String regex)
//replaceFirst(String regex, String replacement)
//replaceAll(String regex, String replacement)

//output
out.println(line);
}
in.close();
out.close();
}
catch(Exception ex){
ex.printStackTrace();
}
}
}

Oliver

Posts: 12
Nickname: lazycat
Registered: Dec, 2002

Re: Working with text files Posted: Apr 21, 2003 2:09 AM
Reply to this message Reply
sorry for submit so many identical reply. there some wrong with my computer.

Flat View: This topic has 4 replies on 1 page
Topic: Req. help in java RB tree (red black) Previous Topic   Next Topic Topic: compiler error. cannot resolve symbol constructor Ex4_10(int)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use