Results 1 to 8 of 8
Thread: Modifying text file
- 04-07-2009, 07:19 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 25
- Rep Power
- 0
Modifying text file
Here is my program. I try to append some whitespace after each word, and copy in a different file. However, it only writes last word and the whitespace in the new file. Anybody has a comment?
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
class ReadFile {
public static void main(String[] args) {
/*
System.out.println("enter the source");
InputStreamReader source = new InputStreamReader(System.in);
*/
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader("d:in.txt")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (s.hasNext()) {
StringBuffer sb = new StringBuffer(s.next());
String a= sb.append("\t \r\n\t /*heey*/").toString();
byte[] b= a.getBytes();
writeFile(b);
}
}
public static void writeFile (byte[] z){
FileOutputStream out = null;
try {
out = new FileOutputStream("dut.txt");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
out.write(z);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
Add to right2001's Reputation
- 04-07-2009, 07:26 AM #2
Member
- Join Date
- Feb 2009
- Posts
- 25
- Rep Power
- 0
I guess, since outputstream is created again and again, FileOutPutStream should be declared before scanning. That may prevent from processing only the last word. Is that right? Does anybody have an idea about how to handle that?
Best,
Murat.
- 04-07-2009, 08:12 AM #3
Can you please tell me what is the objective of your program?
Why i am asking this is,i was not able to understand your code.Mak
(Living @ Virtual World)
- 04-07-2009, 08:53 AM #4
Member
- Join Date
- Feb 2009
- Posts
- 25
- Rep Power
- 0
I try to create a parsing test suite by adding white spaces after each space, new line, ',', and '='. New version is below. There is still one problem. It adds the delimiter at the end of file on each iteration.
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
class ReadFile {
public static void main(String[] args) {
/*
System.out.println("enter the source");
InputStreamReader source = new InputStreamReader(System.in);
*/
FileOutputStream out1 = null;
try {
out1 = new FileOutputStream("d:out1.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader("d:in.txt")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (s.hasNext()) {
StringBuffer sb1 = new StringBuffer(s.next());
String a= sb1.append("\t \r\n\t /*heey*/\r\n").toString();
byte[] b= a.getBytes();
try {
out1.write(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileOutputStream out2 = null;
try {
out2 = new FileOutputStream("d:out2.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Scanner s2 = null;
try {
s2 = new Scanner(new BufferedReader(new FileReader("d:out1.txt"))).useDelimiter(",");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (s2.hasNext()) {
StringBuffer sb2 = new StringBuffer(s2.next());
String c= sb2.append("\t \r\n\t /*heey*/\r\n ,\r\n").toString();
byte[] d= c.getBytes();
try {
out2.write(d);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileOutputStream out3 = null;
try {
out3 = new FileOutputStream("d:out3.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Scanner s3 = null;
try {
s3 = new Scanner(new BufferedReader(new FileReader("d:out2.txt"))).useDelimiter("=");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (s3.hasNext()) {
StringBuffer sb3 = new StringBuffer(s3.next());
String e= sb3.append("\t \r\n\t /*heey*/\r\n =\r\n").toString();
byte[] f= e.getBytes();
try {
out3.write(f);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
- 04-07-2009, 09:01 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 22
- Rep Power
- 0
Yes u are right.
Your FileOutputStream should be initialised only once and it should be before
while (s.hasNext()) .. line
- 04-07-2009, 10:48 AM #6
As told by faisalcmpm,FileOutputstrem should be initialized only once.
Hence,Use a simple way to solve the problem instead of complex one like this.Mak
(Living @ Virtual World)
- 04-07-2009, 11:56 PM #7
Member
- Join Date
- Feb 2009
- Posts
- 25
- Rep Power
- 0
Yeah, I solved the problem, but at this time there appear additional characters at the end of outputfile. Can anybody look at the code and find the bug? It is supposed to just scan the input file, and whenever finds a whitespace, comma or equal sign, should append a sequence of character pattern,and then write to the outputfile. Here is the code:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
class ReadFile {
public static void main(String[] args) {
/*
System.out.println("enter the source");
InputStreamReader source = new InputStreamReader(System.in);
*/
FileOutputStream out1 = null;
try {
out1 = new FileOutputStream("d:out1.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader("d:in.txt")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (s.hasNext()) {
StringBuffer sb1 = new StringBuffer(s.next());
String a= sb1.append("\t \r\n\t /*heey*/\r\n").toString();
byte[] b= a.getBytes();
try {
out1.write(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileOutputStream out2 = null;
try {
out2 = new FileOutputStream("d:out2.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Scanner s2 = null;
try {
s2 = new Scanner(new BufferedReader(new FileReader("d:out1.txt"))).useDelimiter(",");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (s2.hasNext()) {
StringBuffer sb2 = new StringBuffer(s2.next());
String c= sb2.append("\t \r\n\t /*heey*/\r\n ,\r\n").toString();
byte[] d= c.getBytes();
try {
out2.write(d);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileOutputStream out3 = null;
try {
out3 = new FileOutputStream("d:out3.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Scanner s3 = null;
try {
s3 = new Scanner(new BufferedReader(new FileReader("d:out2.txt"))).useDelimiter("=");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (s3.hasNext()) {
StringBuffer sb3 = new StringBuffer(s3.next());
String e= sb3.append("\t \r\n\t /*heey*/\r\n =\r\n").toString();
byte[] f= e.getBytes();
try {
out3.write(f);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
Here is the sample input/output.
input
dabdsad asdskdj
sds=abc
adsa,dsfdff
output
dabdsad
/*heey*/
asdskdj
/*heey*/
sds
/*heey*/
=
abc
/*heey*/
adsa
/*heey*/
,
dsfdff
/*heey*/
/*heey*/
,
/*heey*/
=
Additionally, is there a way to avoid creating multiple files in order to get final output?
Cheers,
Murat.
- 04-08-2009, 01:18 AM #8
I did not feel like peddling through your code, but I do have a few suggestions to help you towards getting an answer.
Learn about java's code conventions. Using a convention will help you and others understand your code.
Since I can't post a link, try searching google for java code conventions
Try to maintain the formatting when you post by using the
[ code ][ /code ] tags without the spaces.
Also try to use descriptive variable names.
Similar Threads
-
count character in text file as input file
By aNNuur in forum New To JavaReplies: 7Last Post: 03-25-2010, 04:01 PM -
Modifying JSP using IntelliJ IDEA 6.0.5
By ddeokarb in forum IntelliJ IDEAReplies: 0Last Post: 11-28-2008, 01:26 PM -
find and replace text from a text file
By gezzel in forum New To JavaReplies: 2Last Post: 09-19-2008, 04:04 PM -
I would like some help lightly modifying a java emulator
By oasis in forum Java AppletsReplies: 1Last Post: 03-27-2008, 07:40 PM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks