Problem with try to write the data ?

Hai,

I read the data from one text file. After reading the data I have modified the data with some special char. Now I would like to write the data into another text file.But I am not able to write the data into new text file.

Please tell me the right way to modified the code

I give my code?br>

class ReadFile

{

publicstaticvoid main(String [] args)

{

ReadFile read =new ReadFile();

read.Read();

System.out.println();

System.out.println("Before write into file");

read.WriteFile();

}

publicvoid Read()

{

String readFile = null, temp =null;

BufferedReader in;

System.out.println("Contents of the file");

System.out.println();

try

{

in =new BufferedReader(new FileReader("c:\\sample.txt"));

readFile = in.readLine();

temp = nextLine(readFile);

System.out.println("The Content "+temp);

in.close();

}

catch(IOException e2)

{

System.out.println("System error");

e2.printStackTrace();

}

}

private String nextLine(String fileName)throws IOException

{

String s ="", line =null;

BufferedReader text;

try{

text =new BufferedReader(new FileReader("c:\\sample.txt"));

line = text.readLine();

while(line !=null)

{

StringTokenizer tokenizer =new StringTokenizer(line);

while(tokenizer.hasMoreTokens())

{

s =s+"$$"+ tokenizer.nextToken();

}

line = text.readLine();

if(line !=null && !(line.equals("")))

{

line ="$$" + line;

}

}

}

catch(Exception e1)

{

System.out.println("Read next line Error");

e1.printStackTrace();

}

return s;

}

publicvoid WriteFile(){

FileInputStream in =null;

FileOutputStream out =null;

try{

in =new FileInputStream("c:\\sample.txt");

out =new FileOutputStream("c:\\output.txt");

int c;

while ((c = in.read()) != -1){

out.write(c);

}

out.close();

}

catch (Exception e){

e.printStackTrace();

}

}

}

The source file is sample.txt , The desg file is output.txt

Thanks ,

Merlin Roshina

[4608 byte] By [MerlinRosinaa] at [2007-10-3 3:46:32]
# 1
Hi try this out = new FileOutputStream("c:\\output.txt", true); bye for nowsat
AnanSmritia at 2007-7-14 21:43:16 > top of Java-index,Java Essentials,Java Programming...
# 2

your read/write methods seem to be erronous.

try this :

try {

BufferedReader in = new BufferedReader(new

FileReader("infilename"));

BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));

String str;

String line;

while ((str = in.readLine()) != null) {

line = process(str);

out.write(line);

}

in.close();

out.close();

} catch (IOException e) {

}

in process method, you modify the line you have just read from the input file

hth

java_2006a at 2007-7-14 21:43:16 > top of Java-index,Java Essentials,Java Programming...
# 3
Hi I tried ..but it writes only the original content but I want to write the modified data..Please give me any alternative ways.. ThxMerlin Roshina
MerlinRosinaa at 2007-7-14 21:43:16 > top of Java-index,Java Essentials,Java Programming...
# 4
Please specify what this program is intended to do.
ejpa at 2007-7-14 21:43:16 > top of Java-index,Java Essentials,Java Programming...
# 5
Hi,You have not called the function nextLine(String fileName)throws IOException in your main function. Call that method and then write.bye for nowsat
AnanSmritia at 2007-7-14 21:43:16 > top of Java-index,Java Essentials,Java Programming...
# 6
Hi ,Please ignore my previous post. I didnt check your code .bye for nowsat
AnanSmritia at 2007-7-14 21:43:16 > top of Java-index,Java Essentials,Java Programming...
# 7
@OP: If your intention is to read input from one file and write the same (also added with other content) to another file, then reply#2 should be working for you.
MadanKumara at 2007-7-14 21:43:16 > top of Java-index,Java Essentials,Java Programming...
# 8

This is developing for upload the data from text file into oracle table. Before uploading modify the original data into some other format. The original data is as a table format (rows and columns). So I read the data from text file and modify the data like

Before modify

Extension : 1190

Name : Viswanath MuthuDat

Date TimeTrunkCalled NumberLocation DurationCost

-

23/08/2006 00:5510296 011380672675311DEFAULT 00:00:480.36

23/08/2006 00:5910296 011380672675311DEFAULT 00:02:541.08

after modified

1190$$ViswanathMuthu$$23/08/2006$$00:55$$10296$$011380672675311$$DEFAUL$$00:00:48$$0.36

1192$$Balakrishnan H$$23/08/2006$$05:47$$10291$$19145748167$$USA-STD$$00:32:56$$11.88

I read the data from this file and upload into oracle table?

Now my problem is try to write the data into a file it will not write the modified content.

Please help me how to write the modified content

Thanks

MerlinRosinaa at 2007-7-14 21:43:16 > top of Java-index,Java Essentials,Java Programming...
# 9
What is the precise text and stack dump of the exception that is thrown?the one that your sample code above seems to be swallowing?I am assuming that this isn't what your real code looks like.If it is, please step away from the computer now slowly ...
ejpa at 2007-7-14 21:43:16 > top of Java-index,Java Essentials,Java Programming...
# 10

You have no connection between the data that you read and modify in your Read- and nextLine-methods and the Write-method.

You have to store the modified text somehow, i.e. in a StringBuffer, and then write the StringBuffer on a new file.

What you are doing now is that you read from the file into a String-object and then in your read-method, you open the file for reading. Even if you modify the String-object that you've got from the file, you do not modify the file itself.

Also, you are only reading the first line of the file...

Lajma at 2007-7-14 21:43:16 > top of Java-index,Java Essentials,Java Programming...
# 11
Hi,Your modified content is stored in temp. So try to save the modified contents somewhere and write that content in a file . bye for nowsat
AnanSmritia at 2007-7-14 21:43:16 > top of Java-index,Java Essentials,Java Programming...
# 12

Something like this...

package lime.javaforum;

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.util.StringTokenizer;

class ReadFile {

StringBuffer sb = new StringBuffer();

public static void main(String[] args) {

ReadFile read = new ReadFile();

read.readFile();

System.out.println();

System.out.println("Before write into file");

read.writeFile();

}

public void readFile() {

BufferedReader in;

System.out.println("Contents of the file");

System.out.println();

try {

in = new BufferedReader(new FileReader("c:\\temp\\input.txt"));

String line = in.readLine();

int i = 1;

while (line != null) {

System.out.printf("%d\t%s\n", i, line);

StringTokenizer tokenizer = new StringTokenizer(line);

while (tokenizer.hasMoreTokens()) {

sb.append("$$" + tokenizer.nextToken());

}

sb.append("\n\r");

i++;

line = in.readLine();

}

in.close();

} catch (IOException e2) {

System.out.println("System error");

e2.printStackTrace();

}

}

public void writeFile() {

FileOutputStream out = null;

try {

out = new FileOutputStream("c:\\temp\\output.txt");

String output = sb.toString();

for (int i = 0; i < output.length(); i++) {

out.write(output.charAt(i));

}

out.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

Lajma at 2007-7-14 21:43:16 > top of Java-index,Java Essentials,Java Programming...
# 13

Hai Lajm ,

Its working fine... Thanks lot...!

One more problem is how to read the content of the data again without $$ from the output.txt. Bcos my objective is read the modified data and insert into oracle table. I m searching the string function to extract the content without $$ . but I m not able to find anything related to this

Please tell me anything releated to this...

Thanks Again...

Merlin Roshina

MerlinRosinaa at 2007-7-14 21:43:16 > top of Java-index,Java Essentials,Java Programming...
# 14

Hi,

Try this .

public void split(){

BufferedReader inn = null;

try {

inn = new BufferedReader(new FileReader("c:\\output.txt"));

String line = inn.readLine();

while (line != null) {

System.out.println(line);

line = line.replaceAll("\\$", "");

System.out.println(line);

line = inn.readLine();

}

inn.close();

} catch (IOException e2) {

System.out.println("System error");

e2.printStackTrace();

}

}

}

bye for now

sat

AnanSmritia at 2007-7-14 21:43:16 > top of Java-index,Java Essentials,Java Programming...
# 15

Hi

Thanks to All Java Mem..

Again I have a little problem to read the data from input.txt.

I want show the output.txt as given below

Input.txt

Extension : 1190

Name : John Smith

Date TimeTrunkCalled Number

-

23/08/2006 00:5510296 011380672675311

23/08/2006 00:5910296 011380672675311

Extension : 1191

Name : David

Date TimeTrunkCalled Number

-

25/08/2006 00:5510296 011380672675311

25/08/2006 00:5910296 011380672675311

This is output.txt

1190$$ John Smith $$23/08/2006$$00:55$$10296$$011380672675311

1191$ David$25/08/2006$$00:55$$10296$$011380672675311

After run the program which is posted by earlier . Its read all the content from input.txt.

Please note that point I would like to read the content only.

Thanks

Merlin Roshina

Message was edited by:

MerlinRosina

MerlinRosinaa at 2007-7-21 10:17:57 > top of Java-index,Java Essentials,Java Programming...
# 16

Hi,

Thanks to all Java Mem..

Again I have a little problem to read the data from input.txt.

I want show the output.txt as given below

Input.txt

Extension : 1190

Name : John Smith

Date TimeTrunkCalled Number

-

23/08/2006 00:5510296 011380672675311

23/08/2006 00:5910296 011380672675311

Extension : 1191

Name : David

Date TimeTrunkCalled Number

-

25/08/2006 00:5510296 011380672675311

25/08/2006 00:5910296 011380672675311

This is output.txt

1190$$ John Smith $$23/08/2006$$00:55$$10296$$011380672675311

1191$ David$25/08/2006$$00:55$$10296$$011380672675311

After run the program which is posted earlier by Lajm. Its read all the content from input.txt.

Please note that point I would like to read the content only.

Thanks

Merlin Roshina

MerlinRosinaa at 2007-7-21 10:17:57 > top of Java-index,Java Essentials,Java Programming...
# 17
Hi,Change line.startswith(" ") or line.startswith("--"). skip the execution.Otherwise do your work. I assume the input.txt format is always same.Check the syntax of startsWith().bye for nowsat
AnanSmritia at 2007-7-21 10:17:57 > top of Java-index,Java Essentials,Java Programming...
# 18
reply #2 will definately work for u.....
Ravijia at 2007-7-21 10:17:57 > top of Java-index,Java Essentials,Java Programming...
# 19

hi Lajm,

I have see ur code alry,,,, but me also cant solve my coding well....

hope Lajm help me .

My problem is : Me wan edit or add text into txt. file, also wan set value where the text wan to add or edit.After that, save it txt. file into save location. izzit can help me solve it? anyways , thanks 1st.

mike_yonga at 2007-7-21 10:17:57 > top of Java-index,Java Essentials,Java Programming...
# 20

Mike: I suppose it's possible that Lajm is waiting, still, for those 10 ducks offered last

August. Possible, but there's only a vanishingly small probability given the fact that he or she hasn't posted since last February.

A better tactic might be to post your own code, and your own question in your own thread. The odds get even better if standard words are used ("your", "already", "want" and the rest): they show that you care, and that's sort of a prerequisite if you expect anybody else to.

pbrockway2a at 2007-7-21 10:17:57 > top of Java-index,Java Essentials,Java Programming...