Java Learning Day 17

1. byte stream:

The FileInputStream (input stream of bytes)
unique method:
the FileInputStream new new FIS = the FileInputStream ( "temp.txt");
1.int fis.available NUM = (); // Returns the total number of bytes of the file
2.fis.read ( ) // returns an int type number, if not read the byte, -1

 1 //使用byte数组进行优化
 2 public static void m3()throws IOException
 3 {
 4 
 5 FileInputStream fis = new FileInputStream("temp.txt");
 6 
 7 byte[] arr=new byte[10];
 8 int len=0;
 9 while((len=fis.read(arr))!=-1){
10 
11 System.out.print(new String(arr,0,len));
12 }
13 
14 fis.close();
15 
16 
17 }

 

1  // byte stream read files: one for a byte 
2  public  static  void M2 () throws IOException
 . 3  {
 . 4 the FileInputStream FIS = new new the FileInputStream ( "temp.txt" );
 . 5  
. 6  int NUM;
 . 7  the while (( fis.read = NUM ()) = -. 1! ) {
 . 8  
. 9 of System.out.print (( char ) NUM);
 10  }
 . 11  
12 is  fis.close ();
 13 is  
14 }

2.FileOutputStream (output byte stream)

方法:
FileOutputStream fos = new FileOutputStream("temp.txt");

fos.write ( "abc" .getBytes () ); // byte [] getBytes () coding
Note: 1. When the output should be carried out to flush after each input (), if not refreshed, not prints
   2. must close at the end of the program (), close the file, and close () default auto-refresh


3. byte stream buffer : BufferedInputStream BufferedOutputStream
  these methods will throw an exception.

. 1  embodiment:
 2  Import the java.io. * ;
 . 3  class Demo4 
 . 4  {
 . 5  public  static  void main (String [] args) 
 . 6  {
 . 7  // byte stream buffer: BufferedOutputStream The BufferedInputStream
 . 8  
. 9  // Copy Image 
10 BufferedInputStream BIS = null ;
 . 11 BufferedOutputStream The BOS = null ;
 12 is  
13 is  the try {
 14  
15 BIS = new new BufferedInputStream ( new new FileInputStream("tou1.jpg"));
16 bos = new BufferedOutputStream(new FileOutputStream("tou1_copy.jpg"));
17 
18 byte[] arr=new byte[1024];
19 int len=0;
20 while((len=bis.read(arr))!=-1){
21 
22 bos.write(arr,0,len);
23 }
24 
25 }catch(IOException e){
26 e.printStackTrace();
27 }finally{
28 
29 IF (BIS =! null )
 30  the try {
 31 is  bis.close ();
 32 } the catch (IOException E) {
 33 is  the throw  new new a RuntimeException ( "buffer byte stream read off failure" );
 34 is  }
 35  
36  IF (BOS! = null )
 37 [  the try {
 38 is  bos.close ();
 39 } the catch (IOException E) {
 40  the throw  new new a RuntimeException ( "byte buffered output stream off failure" );
 41  }
 42  }
 43 is  
44 is  }
 45 }

 

4. standard input (keyboard and association): the System.in (one byte in the input stream object (the default device associated with the keyboard))
  // int kk.read m = (); // read data from the keyboard blocking the method, as long as no input, waits
  //System.out.print((char)m);
  standard output (console equipment already associated) System.out; (out output is a byte stream object (the default association is the console equipment)) // output is a byte stream object


  Use System.in keyboard input, too much trouble
  System.in implementation code keyboard input code (the readLine ()) function is similar to a read line of BufferedReader
  System.in stream of bytes read ----

  BufferedReader ---- characters read stream

5. commutations:

InputStreamReader: character is flowing from the byte stream bridge
the InputStreamReader (in the InputStream)
the OutputStreamWriter: the OutputStreamWriter (the OutputStream OUT)

 

. 1  embodiment:
 2  class Demo6 
 . 3  {
 . 4  public  static  void main (String [] args) throws IOException
 . 5  {
 . 6  // definition of standard keyboard input byte stream object, or define the FileInputStream () objects 
. 7 the InputStream in = the System.in;
 . 8  
. 9  // use the byte stream the conversion into a character stream flow 
10 the InputStreamReader ISR = new new the InputStreamReader (in);
 . 11  
12 is  // used character stream buffer 
13 is the BufferedReader br = new new the BufferedReader (ISR);
 14  
15   
16  
. 17 // standard definition output byte stream object, or define a FileOutputStream () the object 
18 is PrintStream OUT = the System.out; // is output a byte stream object (and has been associated with the console devices)
 . 19  
20 is  // the byte output stream of characters into an output stream 
21 is the OutputStreamWriter OSW = new new the OutputStreamWriter (OUT);
 22 is  
23 is  // used character stream buffer 
24 BufferedWriter, BW = new new BufferedWriter, (OSW);
 25  
26 is  
27 String Line = null ; // read from the keyboard no end flag 
28  the while ((Line = br.readLine ())! = null ) {
 29  IF ( "over" .equals (Line))
 30 break;
31 bw.write(line);
32 bw.newLine();
33 // bw.flush();
34 //System.out.println(line);//字节流
35 }
36 
37 
38 br.close();
39 bw.close();
40 
41 }
42 }

6.File: Java object path or file for the

Examples of objects:
// an encapsulated into a file or folder type object File
File file = new File ( "E : \\ java \\ day17 practice exercises tt.txt \\");
// file for 2. name change
File file = new File ( "E : \\ java \\ day17 practice exercises", "tt.txt");
// 3 defined separately.
File FF = new new File ( "E: \\ \\ the Java practice day17 practice ");
file file = new new file (FF," tt.txt ");

method:
getName () // get the file name
getPath () // get the path definition is relative then get the opposite, defined absolute obtain absolute
getParent () // returns the abstract pathname of the parent directory pathname string; if this pathname does not name a parent directory, or null.
getAbsolutePath () // get the absolute path
lastModified () // retrieve the last modification time of the file
delete () // delete a file or folder
listRoots () // get the root directory (get the system disk)
List () // get a directory names of all the files and folders is returned String []
listfile () // return file [] all the files and folders in a directory are packaged as the object of file
listFile (FilenameFilter filter)


7.递归:函数自己调用自己,当调用次数太多时,会内存溢出,所有要注意,调用次数不要太多
    注意:要有调用的结束条件

8.PrintStream:字节打印流,继承了OutputStream,字节输出流的功能都具备,增加了打印的功能

向哪些设备输出(目的):
1: File类型的文件
2:OutputStream类型的字节输出流
3:字符串类型的文件名
例:ps.write(353);// 00000000 00000000 00000001 01100001---》砍掉前三个字节 01100001


9.父类是Writer
  PrintWriter:字符打印流,字符输出流的基本功能都具备,增加了打印功能
    向哪些设备输出(目的):
    1:File类型的文件
    2:字节输出流对象
    3:字符输出流对象
    4:字符串类型的文件名

 1 例:
 2 
 3 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 4 
 5 // BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
 6 
 7 PrintWriter pw = new PrintWriter(System.out,true);//加上第二个参数,则会自动刷新
 8 
 9 String line = null;
10 while((line=br.readLine())!=null){
11 if("over".equals(line))
12 break;
13 pw.println(line);//会输出换行
14 //pw.write(line);
15 // pw.flush();
16 }

 

Guess you like

Origin www.cnblogs.com/demain/p/11409735.html