Results 1 to 4 of 4

Thread: read from afile

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    23

    Unhappy 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

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    23

    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

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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
  •  



Click Here to Expand Forum to Full Width