• Post Reply Bookmark Topic Watch Topic
  • New Topic

io problem

 
mk singh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ranchers,
i have one problem with , i want to read one file , the contents of the file is like this,
file name: 1.txt
name age date of birth
x 21 82
y 23 80
i want to read the file then i, want to manipulate age like 21+3 and want to write in new file 2.txt after manipulation.
please help me
thanks
 
Riaz Mohamed
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I'm sorry i just had a few minutes and i could complete u'r pprogram by hardcoding the value of the character position ..u could use a while or for loop for this instead, when u use io it reads character by character so u should add add digit wise
hope this helps ..u should be able to fine tune this program i'm givin below for u'r use
import java.io.*;
import java.util.*;
public class readwrite
{
public static void main(String[] ar)
{
try
{
// initailize buffer reader
BufferedReader in = new BufferedReader(new FileReader("1.txt"));
//initialize buffer writer
BufferedWriter out = new BufferedWriter(new FileWriter("2.txt"));

String str;
String str1;
//read from file 1.txt
while((str=in.readLine())!=null)
{

//return ascii value for char at position 3
int tt=((int)str.charAt(3)+4);
//cast to chatr and get the value from ascii
char c=(char)tt;
//replace string with the specific value
str1=str.replace(str.charAt(3),c);
//write into 2.txt
out.write(str1);
}

in.close();
out.close();
}
catch(IOException e)
{

}

}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!