duch11 / 39_read_from_file

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

39 Read from a file

While loop

    class WhileDemo {
        public static void main(String[] args){
            int count = 1;
            while (count < 11) {
                System.out.println("Count is: " + count);
                count++;
            }
        }
    }

Scanner

    Scanner sc = new Scanner(System.in);
    int i = sc.nextInt();

File

    // Create a file object, that can operate on MyTextFile.txt
    // Does not create a file! 
    File file = new File("MyTextFile.txt");
    
    // Create a file if and only if it does not exist
    file.createNewFile();

Scanner, File, loop

      public static void main(String[] args) throws Exception
      {
            Scanner sc = new Scanner(new File("MyTextFile.txt"));
            while (sc.hasNextLong()) {
                 long aLong = sc.nextLong();
            }
      }
    

Scanner methods

    
    hasNext()
    hasNextLine()
    hasNextInt()
    hasNextDouble()        

``

About


Languages

Language:Java 100.0%