• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Readind and Writing Contents of JTable in txt file

 
Simran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Can anybody tell me how can contents of JTable be written into txt file and read from txt file back to new JTable using IO streams.Please help me if anybody knows.
Thanks
Simran
 
raghavender rao
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ther,,,

hope this will help u....
----------------------------------
customers.dat
----------------------------------
Id|Name|City|Phone
102|Rao S|New York|(212)5558725
111|VJ K|India|(0091)9848036500
116|Nir|UK|(435914)5553817
120|Anand|Aus|(91334)5553476
131|Ritesh|NewZealand|(55616)5559811

-----------------------------------------------------------------
DataFileTableModel.java
-----------------------------------------------------------------

if u have any problems dont hesitate to mail me back

Rao
(added code tags)
[This message has been edited by Marilyn deQueiroz (edited October 05, 2001).]
 
Simran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi raghavender ,
your solution really worked .
But while writing the cotents of JTable in file ,i want this type of format.
Id Name City Phone
102~ RaoS~ NewYork~ (212)5558725~
111~ VJK~ India~ (0091)9848036500~
116~ Nir~ UK~ (435914)5553817~
120~ Anand~ Aus~ (91334)5553476~
131~ Ritesh~ NewZealand~ (55616)5559811~
~ sign at back and one alignment for each string in new coulmn, so there must be spaces appended after every string as shown above.
waiting for your reply.
Thanks.
Simran
 
Simran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry alignment is this way:
Id Name City Phone
102~ RaoS~ NewYork~ (212)5558725~
111~ VJK~ India~ (0091)9848036500~
116~ Nir~ UK~ (435914)5553817~
120~ Anand~ Aus~ (91334)5553476~
131~ Ritesh~ NewZealand~ (55616)5559811~
first letter of each string in new column should follow the same line.
Thanks.
Simran
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simran,
Please read http://www.javaranch.com/name.jsp and reregister. We would like to continue to have your presence here on JavaRanch.
 
raghavender rao
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi simran,
try this....
FileInputStream fin = new FileInputStream(datafile);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
// extract column names

StringTokenizer st1 = new StringTokenizer(br.readLine(), " ");
while(st1.hasMoreTokens())
columnNames.addElement(st1.nextToken());

// extract data
while ((aLine = br.readLine()) != null) {

StringTokenizer st2 = new StringTokenizer(aLine, "~ ");

while(st2.hasMoreTokens())
data.addElement(st2.nextToken());
}
if u still have problems do mail me back
Rao
 
raghavender rao
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i thaught it was for reading anyway the above one is for reading n this is for writing i hope this wil solve ur problem...
import java.io.*;
public class AppendFile {
public static void main(String args[]){

int id =101;
String name = "Rao";
String city = "Hyderabad";
String phone = "006173953797";

try {

PrintStream out = new PrintStream(new AppendFileStream("myfile.txt"));

for(int i=0;i<5;i++){

if(i == 0){

out.print("id ");
out.print("name ");
out.print("city ");
out.print("phone ");
out.println();


}else{

out.print(id+"~ ");
out.print(name+"~ ");
out.print(city+"~ ");
out.print(phone+"~ ");
out.println();

}

}

} catch(Exception e) {

System.out.println(e.toString());

}


}


}
class AppendFileStream extends OutputStream {


RandomAccessFile fd;


public AppendFileStream(String file) throws IOException {

fd = new RandomAccessFile(file,"rw");
fd.seek(fd.length());
}

public void close() throws IOException {


fd.close();

}
public void write(byte[] b) throws IOException {
fd.write(b);
}
public void write(byte[] b,int off,int len) throws IOException {


fd.write(b,off,len);


}
public void write(int b) throws IOException {

fd.write(b);


}
}

Rao
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic