|
-
Apr 14th, 2006, 02:19 PM
#1
Thread Starter
Junior Member
read from afile
how i can read the content of certain file
and if the input of files is numbers and it is seprate by (,) how i can get this number one by one
example
2,2,4
how i can get it as following
2
2
4
as number in another aoutput files
-
Apr 14th, 2006, 06:10 PM
#2
Re: read from afile
Code:
public static void main (String[] args) {
try {
java.util.StringTokenizer st = new java.util.StringTokenizer(new java.io.
BufferedReader(new java.io.FileReader("C:\\txt.txt")).readLine(), ",") ;
int[] ints = new int[st.countTokens()] ;
int i = 0 ;
while (st.hasMoreTokens()) {
ints[i] = Integer.parseInt(st.nextToken()) ;
i++ ;
}
}
catch (java.io.IOException ex) {
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Apr 16th, 2006, 03:16 AM
#3
Thread Starter
Junior Member
Re: read from afile
i make your code and it work right but it take first line in the file only and i donot know why
-
Apr 16th, 2006, 12:34 PM
#4
Re: read from afile
Code:
import java.io.* ;
import java.util.* ;
public class Sample
{
public static void main (String[] args) {
try {
BufferedReader read=new BufferedReader(new FileReader("C:\txt.txt"));
String s="";
StringTokenizer st ;
while(read.ready()){
s+=read.readLine();
}
read.close();
st= new StringTokenizer(s, ",\n") ;
int[] ints = new int[st.countTokens()] ;
int i = 0 ;
while (st.hasMoreTokens()) {
ints[i] = Integer.parseInt(st.nextToken()) ;
i++ ;
}
}
catch (IOException ex) {
}
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|