The Io of input and output operations in Java summarize the three of

  • 2020-04-01 01:21:07
  • OfStack

To be honest, I'm not really a fan of the Java language, even though it's powerful and has a lot of off-the-shelf apis to call
But I always felt that it made the simple things too complicated and sometimes even lost
It is not clear whether it is for writing or for the language itself
The first programming language I studied was Python, although I didn't learn much
But its simplicity and elegance are still memorable.
The second language I came into contact with was C, which gave me a feeling of purity and efficient flexibility
Instead of writing a bunch of cryptic code like Java to implement a small feature
To be frank, if a person is studying something he is not interested in, he will be tired
What supports me is that I have an unusual yearning for mobile development and quite like Android, but the main language for developing Android is Java
I've been learning it for half a year, but I still can't explain what enterprise development is all about
All I want is to treat programming as a flirtation in life, and if you want something, do it, as easily as possible
Even my guess is that, in the future, programming will be a technology like everyone else USES office
So how hard is it for you as programmers?
You know, it's pretty scary when a person can put his heart's best before his real thoughts...
Therefore, everyone should do what they most want to do in the way they like and feel efficient
Well, with all that said, there's really only one purpose
Am I going to tell you that I don't understand and don't want to understand Java's complex garbage syntax?
I only use the simplest and most useful things...
I didn't bother to keep track of the previous Java IO writing, mainly the System class support for IO
If you think my code isn't deep enough, go ahead and spray it, but what else can you do after that?

Now, to get to the point...
In this section we'll look at the use of the Scanner class and the PrintWriter class

Scanner class
Example 1: read from the keyboard
 
import java.util.Scanner; 
public class Demo { 
public static void main(String[] args ) { 
Scanner input = new Scanner(System.in); 
System.out.println(" Please output an integer: "); 
int i = input.nextInt(); 
System.out.println(" The integer you entered is: " + i); 
} 
} 

The above demonstration is just to read an integer, of course, there are methods to read floating point Numbers and other data types, relatively simple, look at the API

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010311523.png ">  
Instance 2: read from a string

 
import java.util.Scanner; 
public class Demo { 
public static void main(String[] args ) { 
//The rn here is a newline character, but in Linux you just use n
Scanner input = new Scanner("hellornworldrn"); 
//Loop read, the hasNext() method is the same as in the collection framework
while(input.hasNext()) { 
//Read one line at a time, other reading methods see API, relatively simple
String s = input.nextLine(); 
System.out.println(s); 
} 
} 
} 

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010311524.png ">
Instance 3: read from a file

 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 
public class Demo { 
public static void main(String[] args ) { 
String path = File.separator + "home" + File.separator + "siu" + 
File.separator + "work" + File.separator + "demo.txt"; 
File f = new File(path); 
Scanner input = null; 
try { 
//Constructing a Scanner object from a file may cause an exception
input = new Scanner(f); 
while(input.hasNext()) { 
String s = input.nextLine(); 
System.out.println(s); 
} 
} catch (FileNotFoundException e) { 
e.printStackTrace(); 
} finally { 
input.close(); 
} 
} 
} 

The important thing to note here is that to create a Scanner object from a File you must first have a File object, although you can do this using anonymous objects
In addition, you need to catch exceptions and close the file stream

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010311525.png ">
PrintWriter class
Example 4: writing to a file

 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.PrintWriter; 
public class Demo { 
public static void main(String[] args) { 
String path = File.separator + "home" + File.separator + "siu" + 
File.separator + "work" + File.separator + "demo.txt"; 
//Create file object
File file = new File(path); 
PrintWriter p = null; 
try { 
//Here the constructor can also pass other objects, specific reference API documentation
p = new PrintWriter(file); 
//Write a line to the file, along with the print() and printf() methods
p.println(" If one day I go back to the past "); 
p.println(" Back to the original me "); 
p.println(" Do you think I'm good "); 
//Refresh the flow
p.flush(); 
} catch (FileNotFoundException e) { 
e.printStackTrace(); 
} finally { 
p.close(); 
} 
} 
} 

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010311526.png ">
There is also a PrintStream class similar to the PrintWriter, which is used as an example because the text file is human-readable
Binary files (in byte mode) require special programs to read them
One might ask: why do you need the PrintWriter and PrintStream classes when FileOutputStream and FileWriter can write files
If you look closely at the API documentation, you can see that the former's pure character and byte write operations are mostly done in arrays
File refinement is very inconvenient, and PrintWriter and PrintStream solve this problem by providing methods like print()
Also, the PrintWriter and PrintStream are created directly if the file object does not exist, if the file object already exists
They overwrite the original file without adding methods

It's also easy to solve this problem. Look at the API documentation
The PrintWriter has a constructor, the PrintWriter(Writer out), that is, the ability to pass in a Writer object
PrintStream has a constructor PrintStream(OutputStream out), that is, an OutputStream object can be passed in
So let's just write it this way
New PrintWriter (new FileWriter (file, true))
New PrintStream (new FileOutputStream (file, true))
You can both add data and process files more efficiently, as shown in the code below

Example 5: implement the data append function of PrintWriter

 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 
public class Demo { 
public static void main(String[] args) { 
String path = File.separator + "home" + File.separator + "siu" + 
File.separator + "work" + File.separator + "demo.txt"; 
//Create file object
File file = new File(path); 
PrintWriter p = null; 
try { 
//The PrintWriter object is built in a FileWriter fashion to implement appending
p = new PrintWriter(new FileWriter(file,true)); 
p.println(" nima   This sentence is an addendum   Didn't see "); 
p.flush(); 
} catch (IOException e) { 
e.printStackTrace(); 
} finally { 
//Let's close the stream carefully. Ok. ^_^
p.close(); 
} 
} 
} 

See, that's going to be the append, the last line is going to be

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010311527.png ">
IO support for the System class
Write in instance 6: System class

 
import java.io.IOException; 
import java.io.OutputStream; 
public class Demo { 
public static void main(String[] args) { 
//Remember, the OutputStream is the parent of all bytes written into the stream
OutputStream out = System.out; 
try { 
//Write data, can only be an array, so use the getBytes() method
out.write("Hello . bitch ! rn".getBytes()); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
} 

Note that the overwrite behavior of system.out is validated here
If you want to learn IO well, you need to understand the polymorphism of the whole IO system

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010311528.png ">
Read in instance 7: System class

 
import java.io.IOException; 
import java.io.InputStream; 
public class Demo { 
public static void main(String[] args) { 
//Remember that the InputStream is the parent of all the byte input streams
InputStream in = System.in; 
System.out.print(" Please enter:  "); 
byte[] buf = new byte[1024]; 
int len = 0; 
try { 
//The input data is guaranteed into the array, and len records the length of the input
len = in.read(buf); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
//Print the data in an array as a string
System.out.println(" Your input is:  " + new String(buf,0,len)); 
} 
} 

Look, this will take the content from the keyboard and print it

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010311529.png ">
Note that the array size here is 1024 bytes
Once more than 1024 bytes of data is entered, the excess will be intercepted, so this program has limitations
Also, a Chinese takes two bytes, and the input Chinese is sometimes accidentally truncated
Believe me, every program is compiled by me ~!!
Instance 8: use BufferedReader to read from the keyboard

 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
public class Demo { 
public static void main(String[] args) { 
BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); 
System.out.print(" Please enter text: "); 
try { 
String str = b.readLine(); 
System.out.println(" What you typed was: " + str); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
//Loop read mode
/* 
while(true) { 
System.out.print(" Please enter text: "); 
String str = null; 
try { 
str = b.readLine(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
//If you type over, you end the loop
if("over".equals(str)) { 
break; 
} 
System.out.println(" What you typed was: " + str); 
} 
*/ 
try { 
//Turn off the flow and throw impatiently
b.close(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
} 

This has the advantage over the previous method: you don't have to worry about the array size
One of the most important methods for a BufferedReader is readLine(), which reads one line at a time
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010311530.png ">


Related articles: