파일 및 텍스트 I/O 연습 문제 (java 실행)
현재 한 학생 명단은 다음과 같다. 그 중에서 학번과 성명 사이를 하나의 빈칸으로 나누고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]);
}
}
}
독자 여러분, 읽어주셔서 감사합니다. 좋아하는 분들은 팔로우 해주세요!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.