Home > Java > javaTutorial > Detailed code explanation of java using redirection method to read or write data from a file

Detailed code explanation of java using redirection method to read or write data from a file

黄舟
Release: 2017-03-22 10:11:53
Original
2397 people have browsed it

This article mainly introduces in detail the use of redirection method to read or write data from files. It has certain reference value. Interested friends can refer to it

Using Redirection Method to read or write data from a file

1.Problem description

First generate 10,000 random numbers, one line for every ten, and put them into the file. (Use redirection method)
Use redirection method to read 10,000 numbers from the file and print out the top ten. (It is not allowed to call the sorting method)

2. The program is as follows:

2.1 Write

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class A{
  
  static void to_txt(){
    for(int i=1;i<=10000;i++){
      System.out.print((int)(Math.random()*10000+1)+" ");
      if(i%10==0) System.out.println();  
    }
  }
    public static void main(String[] args){
    to_txt();
      
  }    
}
Copy after login

2.2 Read

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class A{
  
    static void read_txt(int k){//此处参数表示打印前k项
    List<Integer> lst = new ArrayList<Integer>();
    Scanner scan = new Scanner(System.in);
    String s = scan.nextLine();
    String[] x = s.split(" ");
    lst.add(Integer.parseInt(x[0]));  
    for(int i=1;i<x.length;i++){
      int b = Integer.parseInt(x[i]);
      if(b<lst.get(lst.size()-1)){
        lst.add(b);
        continue;
      }
      for(int j=0;j<lst.size();j++){
        if(b>lst.get(j)){
          lst.add(j,b);
          break;
        }
      }
        
    }
    for(int i=0;i<k;i++){
      
      System.out.print(lst.get(i)+" ");
    }    
  }
  public static void main(String[] args){
    read_txt(10);  
  }    
}
Copy after login

3. Running results

3.1. Enter the console from the Java file location and enter Java file>txt file in the console to write the running results to the specified txt file (to be written file).

At this time, the running result is not displayed on the console, but written to the specified file

Detailed code explanation of java using redirection method to read or write data from a file

3.2. Enter the console at the location of the Java file and control Enter Java + file name < Specify the txt file (the file to be read)

The following figure shows the first ten items of the read data to be output.

Detailed code explanation of java using redirection method to read or write data from a file

The above is the detailed content of Detailed code explanation of java using redirection method to read or write data from a file. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template