Java-Input/Output&Streams

  • two way to store data
    • text format
      • human-readable
      • sequence of characters
    • binary format
      • data item represent as bytes
  • download.png
  • Scanner Class can read input from File or stdin
    • File inFile = new File(“input.txt”);
    • Scanner in = new Scanner(inFile);
    • in.useDelimiter()
      • exp.
        • “[^A-Za-z]+”    ->     al chracter neither capital letter or lower letter will work as delimiter
    • while(in.haveNextLine()){String a = in.nextLine; ….}
      • next(); -> read a word each time, seperate by white space
        • white Spaces
          • ” ” simple space
          • \n newLine
          • \r
          • \t
          • \f
      • nextLine();
        • read entire line and use \n as Delimiter
        • combine with charAt(int) to get sepcific character at a position
        • (!Character.isXX(line.charAt(k))) used to control input as a specific type only each line
        • break string with String.substring(int,int) to get specific part of string
        • remove white space with string.trim
      • hasNextLine();
      • hasNext();
      • nextDouble();
      • nextInt();
    • in.close()
    • control inut type with Character.
      • isDigit -> number
      • isLetter -> upper and lower case letter
      • isUpperCase -> upper case letter
      • isLowerCase -> lower case letter
      • ise WhiteSpace -> ” “, \n, \t…….
  • PrintWriter class can used to write text files
    • PrintWriter out =new PrintWriter(“out.txt”)
      • if out.txt exist, it will cleared
      • if not, create a new
      • out.
        • print(“”) int,double,char…
        • println(“”) only to string
        • printf(format,””) control format of output
      • out.close();
    • System.out is a PrintStream object

留下评论