The first two days to learn some of the knowledge of IO operations, today focus on sorting out, the province later forgot
- Console type IO operations:
General type Read data: Sacnner in=new Sacnner (system.in); String name=in.nextline ()/nextint ()/next ();
Password type read operation: Console Cons=system.console (); String name=cons.readline ("username");
Char[] Pass=cons.readpassword ("Password");
Write operation: System.out.println (String);
- FileReader and FileWriter:
The approximate process is as follows: FileWriter is=new FileWriter ("F:\\work\\js.txt");
Js.write ("I'm Tired");//write operation, at this time in memory
Js.flush ();//Flushes the stream, performs this operation, writes the file, and performs this action automatically
Js.close ();//Close stream
The strict wording is as follows: FileWriter Js=null;
Try
{
Js=new FileWriter ("F:\\work\\javaexer\\jsky.txt", true);//true represents an increase rather than overwriting the text
Js.write ("\ r \ n Working hours are not allowed to sleep");
}
catch (Exception e)
{
SYSTEM.OUT.PRINTLN ("Content write failed");
}
Finally
{
Try
{
if (js!=null) js.close ();
}
catch (Exception e)
{
System.out.println ("Shutdown failed");
}
}
The corresponding read operation is as follows: FileReader Jj=null;
Try
{
Jj=new FileReader ("F:\\work\\javaexer\\jsky.txt");
int SS;
Char[] Msg=new char[1024];
while ((Ss=jj.read (msg))!=-1)
System.out.println (New String (MSG,0,SS));
SYSTEM.OUT.PRINTLN ("read Complete");
}
catch (Exception e)
{
SYSTEM.OUT.PRINTLN ("Error occurred when file is not found or read");
}
Finally
{
Try
{
if (jj!=null) jj.close ();
}
catch (Exception e)
{
SYSTEM.OUT.PRINTLN ("Error occurred during shutdown");
}
}
- Bufferreader and Bufferwriter:
Buffers are designed to increase the operational efficiency of a stream, so there must be a stream object before the buffer is created. It is the enhancement of the convection function on the basis of the flow.
Bufferwriter Operation steps:
FileWriter Fw=null;
BufferedWriter Bw=null;
String fs= "F:\\work\\javaexer\\jsky.txt";
Try
{
Fw=new FileWriter (FS);
Bw=new BufferedWriter (FW);
Bw.write ("\ n is a new week");
Bw.flush ();
}
catch (Exception e)
{
SYSTEM.OUT.PRINTLN ("File not found or file write error");
}
Finally
{
Try
{
if (bw!=null) bw.close ();
if (fw!=null) fw.close ();
}
catch (Exception e)
{
SYSTEM.OUT.PRINTLN ("error at shutdown");
} }
BufferedReader Operation:
FileReader Fr=null;
BufferedReader Br=null;
String fs= "F:\\work\\javaexer\\jsky.txt";
Try
{
Fr=new FileReader (FS);
Br=new BufferedReader (FR);
String Js=null;
while ((Js=br.readline ())!=null) System.out.println (JS);
}
catch (FileNotFoundException e)
{
SYSTEM.OUT.PRINTLN ("File not found! "+e.tostring ());
}
catch (IOException e) {
SYSTEM.OUT.PRINTLN ("File read error" +e.tostring ());}
Finally
{
Try
{
if (br!=null) br.close ();
if (fr!=null) fr.close ();
}
catch (Exception e)
{
SYSTEM.OUT.PRINTLN ("close exception");
}
}
- For non-character types of files, we should use the corresponding byte stream for read and write operations:
Using a byte stream to make a copy of a picture
public static void Main (string[] args)
{
FileInputStream Js=null;
FileOutputStream Sk=null;
Try
{
Js=new FileInputStream ("f:\\work\\temp\\java001.jpg");
- Sk=new FileOutputStream ("f:\\work\\temp\\java002.jpg");
int ss=0;
Byte[] B=new byte[1024];
while ((Ss=js.read (b))!=-1) Sk.write (B,0,SS);
System.out.println ("copy Complete");
}
catch (Exception e)
{
SYSTEM.OUT.PRINTLN ("Open error or write error");
}
Finally
{
Try
{
if (js!=null) js.close ();
if (sk!=null) sk.close ();
}
catch (Exception e)
{
System.out.println ("Shutdown failed");
}
}
Java Basics IO