The Artima Developer Community
Sponsored Link

Java Answers Forum
text files

17 replies on 2 pages. Most recent reply: Nov 7, 2002 3:13 AM by ben

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 17 replies on 2 pages [ 1 2 | » ]
ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

text files Posted: Oct 23, 2002 9:57 AM
Reply to this message Reply
Advertisement
hello is it possible that you could help me?

im trying to read in a text file which is no problem but i also want a comma to work as a delimeter but when i do this it will only read in the first line of text but i want the whole file to read in and be delimeted
can you help me please ?
thanks for your time ben


Ramu

Posts: 29
Nickname: sripoorna
Registered: Oct, 2002

Re: text files Posted: Oct 23, 2002 8:06 PM
Reply to this message Reply
hi
they are two ways to do that

1st way:
private void test1()
{
try
{
FileInputStream fis = new FileInputStream("c:\\test.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);

String str;
while((str = br.readLine()) != null)
System.out.println(str);

}
catch(Exception e)
{
System.out.println("excepton ...."+e);
}
}

2nd way:

private void test()
{
try
{
RandomAccessFile ras = new RandomAccessFile("c:\\test1.txt", "r");
String str;
while((str = ras.readLine()) != null)
System.out.println(str);
}
catch(Exception e)
{
System.out.println("excepton ...."+e);
}
}

ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

Re: text files Posted: Oct 24, 2002 3:02 AM
Reply to this message Reply
hello ramu
thanks for yr time but does it delimeter the file do i still need to put in a string tokenizer and put that though a loop?

Ramu

Posts: 29
Nickname: sripoorna
Registered: Oct, 2002

Re: text files Posted: Oct 24, 2002 3:45 AM
Reply to this message Reply
hi,
in that case use StreamTokenizer.
In this one also we have to read token by token and we have to check whether the token is sting or int etc..

for more u refer to the java.io.StreamTokenizer API

bye
yours
ramu

ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

Re: text files Posted: Oct 24, 2002 9:40 AM
Reply to this message Reply
hello ramu
this is the source code can you put me right where im going wrong please.
what i want is the file work.txt to be read in the get delimeted but it only reads in one line (the first line) and i want it to read in all of the lines i know i need a loop in there to loop though each line where should it go if you could adjust my code so it will do that i would be most gratefull thankyou ben

import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.StringTokenizer.*;

public class ReadThis {

public static void main(String[] args) {
try {
FileReader file = new FileReader("Work.txt");
BufferedReader br = new BufferedReader(file);
StringTokenizer st = new StringTokenizer(br.readLine(), ",");
String line = br.readLine();
boolean eof = false;

while ((!eof)&(st.hasMoreTokens())){
String s = st.nextToken();

if (line == null){

eof = true;
}
else

System.out.println(s);

}
br.close();
}catch (FileNotFoundException fe) {
System.out.println("Error - - " + fe.toString());
} catch (IOException e) {
System.out.println("Error - - " + e.toString());

}
}
}

Ramu

Posts: 29
Nickname: sripoorna
Registered: Oct, 2002

Re: text files Posted: Oct 24, 2002 7:17 PM
Reply to this message Reply
hi

the problem in u program is u readling only one line.
in the while loop u r not reading the next line.
and one more suggestion is u divide u r program into two loops one loop for reading the line one for used stringtokenizing.

i had modified u r code and use it

import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.StringTokenizer.*;

public class ReadThis
{

public static void main(String[] args)
{
try
{
FileReader file = new FileReader("work.txt");
BufferedReader br = new BufferedReader(file);

String str_Temp = br.readLine();
StringTokenizer stoken = null;

while(str_Temp != null)
{
stoken = new StringTokenizer(str_Temp, ",");

while(stoken.hasMoreTokens())
{
String str_Temp1 = stoken.nextToken();
System.out.println(str_Temp1);
}
str_Temp = br.readLine();
}
/* StringTokenizer st = new StringTokenizer(br.readLine(), ",");
String line = br.readLine();
boolean eof = false;

while ((!eof)&(st.hasMoreTokens()))
{
String s = st.nextToken();
if (line == null)
{
eof = true;
}
else
System.out.println(s);
}
*/
br.close();
}
catch (FileNotFoundException fe)
{
System.out.println("Error - - " + fe.toString());
}
catch (IOException e)
{
System.out.println("Error - - " + e.toString());
}
}
}

ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

Re: text files Posted: Oct 25, 2002 1:14 AM
Reply to this message Reply
thankyou very much Ramu it works excellent i appriciate your time thanks again
ben
p.s.
I will come and pester you again sometime (lol)
cheers mate..

ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

Re: text files Posted: Oct 29, 2002 2:14 AM
Reply to this message Reply
hello again Ramu
i have another problem
i want the file to be read in but i only want the first word of each line to be read in. i was going to put it in a string array, would this be a good way of doing it if so could u let me know
thanks ben

ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

Re: text files Posted: Oct 29, 2002 4:00 AM
Reply to this message Reply
hello again Ramu
i have another problem
i want the file to be read in but i only want the first word of each line to be read in. i was going to put it in a string array, would this be a good way of doing it if so could u let me know
thanks ben

Ramu

Posts: 29
Nickname: sripoorna
Registered: Oct, 2002

Re: text files Posted: Oct 29, 2002 4:21 AM
Reply to this message Reply
hi,

sorry for late reply. i was busy with my work.
replace the two while loops with the following single while code

while(str_Temp != null)
{
stoken = new StringTokenizer(str_Temp, ",");
String str_Temp1 = stoken.nextToken();
System.out.println(str_Temp1);
str_Temp = br.readLine();
}

regards
ramu

ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

Re: text files Posted: Oct 30, 2002 2:02 AM
Reply to this message Reply
hello Ramu
thanks for the idea.
when i compile the code with the new while loop in it thows a no such element exception why is this?
ben

ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

Re: text files Posted: Oct 30, 2002 9:50 AM
Reply to this message Reply
hello Ramu
could u please help me with this?
the file that i am reading in will have 10 lines of text with 10 words on each line (seperated by comar delimeters). and wat i want is the first word of each line to be put inside a hash table and then the second word of each line into the same hashtable and so on with the 3rd word 4,5,6,7,8,9,10and then at the end i want to read the hashtable out.
could u please give me any suggestions
thanks for yr time ben

ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

Re: text files Posted: Oct 31, 2002 1:40 AM
Reply to this message Reply
hello Ramu
could u please help me with this?
the file that i am reading in will have 10 lines of text with 10 words on each line (seperated by comar delimeters). and wat i want is the first word of each line to be put inside a hash table and then the second word of each line into the same hashtable and so on with the 3rd word 4,5,6,7,8,9,10and then at the end i want to read the hashtable out.
could u please give me any suggestions
thanks for yr time ben

Ramu

Posts: 29
Nickname: sripoorna
Registered: Oct, 2002

Re: text files Posted: Nov 1, 2002 9:09 PM
Reply to this message Reply
hi ben,

i had compiled the text file program with the loop provided earlier.
it is working fine. it is not giving any exception.
Here i am attaching the program pls go though it.

import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.StringTokenizer.*;

public class ReadThis
{
public static void main(String[] args)
{
try
{
FileReader file = new FileReader("work.txt");
BufferedReader br = new BufferedReader(file);

String str_Temp = br.readLine();
StringTokenizer stoken = null;

while(str_Temp != null)
{
stoken = new StringTokenizer(str_Temp, ",");
String str_Temp1 = stoken.nextToken();
System.out.println(str_Temp1);
str_Temp = br.readLine();
}
br.close();
}
catch (FileNotFoundException fe)
{
System.out.println("Error - - " + fe.toString());
}
catch (IOException e)
{
System.out.println("Error - - " + e.toString());
}
}
}

if any quries let me know

cheers mate
ramu

Ramu

Posts: 29
Nickname: sripoorna
Registered: Oct, 2002

Re: text files Posted: Nov 1, 2002 9:12 PM
Reply to this message Reply
hi,

Basically a hastable will store a key-value pair so in u r case what about the key?

regards
ramu

Flat View: This topic has 17 replies on 2 pages [ 1  2 | » ]
Topic: Java help please Previous Topic   Next Topic Topic: Help please

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use