Read the contents of the file as input to the java program

Header file: import java.io.FileReader;

method one:

        1.BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

       2. reader = new BufferedReader(new FileReader("D:\\workspace\\nfv\\work"));
          String tempString = null;

 while ((tempString = reader.readLine()) != null) {

         。。。。

              }


Method two:

         FileInputStream fis=new FileInputStream("work");  

         System.setIn(fis); //input redirection (if you don't need redirection, don't use it)

         Scanner sc=new Scanner(System.in);//Create an instance of Scanner object

          while(sc.hasNextLine())  
        {  

。。。。

}

1.Scanner is a simple text scanner that can use regular expressions to analyze basic types and strings, and write data directly to the hard disk; Scanner obtains input data based on space characters : such as pressing the space bar, Tab key or Enter key , Scanner will return the next input. Scanner can't enter spaces, if you want to get a string containing spaces BufferedReader can do it.
BufferedReader reads text from a character input stream, buffering individual characters to provide efficient reading of characters, arrays, and lines.
2. System.in: The object of the InputStream class implements standard input, and its read method can be called to read the keyboard data.
System.out: The object of the PrintStream print stream class implements standard output, and its print, println or write methods can be called to output various types of data.
3. About redirection
 
 

Java 's standard input/output is represented by System.in and System.out, respectively, by default, it represents the keyboard and the display. When a program obtains input through System.in, it actually obtains input through the keyboard. When a program performs output via System.out, the program always outputs to the screen.

Three methods for redirecting standard input/output are provided in the System class

static void setErr(PrintStream err) redirects the "standard" error output stream

static void setIn(InputStream in) redirects the "standard" input stream

static void setOut(PrintStream out) redirects the "standard" output stream

下面程序通过重定向标准输出流,将System.out的输出重定向到文件输出,而不是在屏幕上输出。

[java]  view plain  copy
  1. import java.io.FileOutputStream;  
  2. import java.io.PrintStream;  
  3. public class Test {  
  4.     public static void main(String[] args) throws Exception  
  5.     {  
  6.           
  7.         PrintStream ps=new PrintStream(new FileOutputStream("work"));  
  8.         System.setOut(ps);  
  9.         System.out.println("Hello World!");  
  10.   
  11.     }  
  12.       
  13.   
  14.   
  15.       
  16. }  

下面的代码将System.in重定向到文件输入,所以将不接受键盘输入

[java]  view plain  copy
  1. import java.io.FileInputStream;  
  2. import java.util.Scanner;  
  3.   
  4.   
  5. public class Test {  
  6.     public static void main(String[] args) throws Exception  
  7.     {  
  8.         FileInputStream fis=new FileInputStream("work");  
  9.         System.setIn (fis);  
  10.           
  11.         Scanner sc=new Scanner(System.in);  
  12.         while(sc.hasNextLine())  
  13.         {  
  14.             System.out.println(sc.nextLine());  
  15.         }  
  16.           
  17.   
  18.     }  
  19.       
  20.   
  21.   
  22.       
  23. }  
The redirected content is referenced from: http://blog.csdn.net/zhy_cheng/article/details/7891142



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325681815&siteId=291194637