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
Printable View
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
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 make your code and it work right but it take first line in the file only and i donot know why
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) {
}
}
}