파일 및 텍스트 I/O 연습 문제 (java 실행)

23403 단어
제목 설명
현재 한 학생 명단은 다음과 같다. 그 중에서 학번과 성명 사이를 하나의 빈칸으로 나누고PrintWriter로 이 내용을 d:\a.txt에서 180596256왕정 18056201 리웨이 185632925 조해연 18756561523 리설주를 사용하여 하드디스크에 학생 성적표를 작성하고 저장 위치 d:\b.txt 180596256 왕정 100 18056201 리웨이 90 185632925 조해연 1875 65 61523 리설주 87 코드를 실현하였다.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.io.*;
public class ReplaceText {
	public static String path = "D:\\Java\\";
	public static void main(String[] args)throws IOException{
				File  stu=new  File("d:\\a.txt");
				try{
				PrintWriter  write = new  PrintWriter("d:\\a.txt");
				write.println("180596256   ");
				write.println("180562301   " );
				write.println("185632925    ");
				write.println("187561523    ");
			
				write.close();
				}catch(Exception e){
					System.out.println("Oh!NO");
				}
				String s = "d:\\a.txt";
				Scanner input = new Scanner(new File(s));
				while (input.hasNext()) {
				String s1 = input.nextLine();
				System.out.println(s1);
							}
				input.close();
				File  score=new  File("d:\\b.txt");
				try{
					PrintWriter  Write1 = new  PrintWriter(score);
					Write1.println("180596256    100");
				Write1.println("180562301    100" );
				Write1.println("185632925     100");
				Write1.println("187561523     100");

				}catch (Exception e){
					System.out.println("Oh!NO");	
				}
				 
					   Scanner c = new Scanner(stu);
						  while(c.hasNext()){
							   String num = c.next();
							   String name = c.next();
							   String filename = "  "+num+"  "+name;
							   File f1 = new File(path +filename);
							   if(!f1.exists()){
							   f1.mkdirs();}
							   File f2 = new File(f1,"first.txt");
							   if(!f2.exists()){
							   f2.createNewFile();}
							   PrintWriter output  = new PrintWriter(f2);
							   Scanner score1 = new Scanner(score);
							   while(score1.hasNext()){
								   String s1 = score1.nextLine();
								   output .println(s1);
							   }
							   score1.close();
							   output.close();
						  }
				   }
				}

제목 설명
컴파일러, C디스크에 파일 홈 워크를 만듭니다.txt, 무작위로 100개의 0~100 사이의 정수를 생성하고 텍스트 I/O로 파일에 기록합니다.파일의 정수는 공백으로 나누어져 있으며, 파일에서 데이터를 읽고 정렬된 데이터를 표시합니다.코드 구현
import java.util.*;
import java.io.*;
public class suijishu {
	public static void main(String[] args)throws Exception 	
		   	{ 
		   		 File file = new File("d:\\homework.txt");
		   		 if(!file.exists()){
		   			 file.createNewFile();
		   		 }
		   		 PrintWriter output = new PrintWriter(file);
		   		 for(int i=0;i<100;i++){
		   			  int a=(int)(Math.random()*100);
		   			 output.print(a);
		   			  output.print(" ");
		   		 }
		   		 output.close();
		   		String s = "D:\\homework.txt";
				Scanner input = new Scanner(new File(s));
				while (input.hasNext()) {
				String s1 = input.nextLine();
				System.out.println(s1);
							}
				input.close();
		   		 Scanner w = new Scanner(file);
		   		 int [] num = new int[100];
		   		 while(w.hasNext()){
		   			 for(int i = 0;i<100;i++){
		   				 num[i] = w.nextInt();
		   			 }
		   		 }
		   		 w.close();
		   		 Arrays.sort(num);
		   		 for(int i = 0;i<100;i++){
		   			 System.out.println(num[i]);
		   		 }
		   	}
		   }

독자 여러분, 읽어주셔서 감사합니다. 좋아하는 분들은 팔로우 해주세요!

좋은 웹페이지 즐겨찾기