aspose file tools*
The moose likes I/O and Streams and the fly likes Readind and Writing Contents of JTable in txt file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Readind and Writing Contents of JTable in txt file" Watch "Readind and Writing Contents of JTable in txt file" New topic
Author

Readind and Writing Contents of JTable in txt file

Simran
Greenhorn

Joined: Oct 01, 2001
Posts: 7
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

Joined: Jul 20, 2001
Posts: 45
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).]


Rao<BR>Sun Certified Programmer for the Java��� 2 Platform
Simran
Greenhorn

Joined: Oct 01, 2001
Posts: 7
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

Joined: Oct 01, 2001
Posts: 7
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

Joined: Jul 22, 2000
Posts: 9046
    
  10
Simran,
Please read http://www.javaranch.com/name.jsp and reregister. We would like to continue to have your presence here on JavaRanch.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
raghavender rao
Ranch Hand

Joined: Jul 20, 2001
Posts: 45
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

Joined: Jul 20, 2001
Posts: 45
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Readind and Writing Contents of JTable in txt file