java作业:输入输出课后习题(第十次作业)

1、目录、文件操作

(1)在d盘下建立一个目录dir1

(2)在目录dir1下建立文本文件1.txt,并在里面输入内容。

(3)输出1.txt文件的大小及最后修改日期。

(4)将1.txt重命名为2.txt。

(5)将目录dir1删除。(注意file.delete()只能删除文件以及空目录)

import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) throws Exception{
		//(1)在d盘下建立一个目录dir1
		File file=new File("d:/dir1");
		file.mkdir();
		//(2)在目录dir1下建立文本文件1.txt。
		File file1=new File("d:/dir1/1.txt");
		file1.createNewFile();
		//并在里面输入内容
		FileWriter file2 = new FileWriter("d:/dir1/1.txt");
		Scanner cin = new Scanner (System.in);
        String s = cin.next();//输入数据
        file2.write(s.toCharArray());
        file2.close();
        //(3)输出1.txt文件的大小及最后修改日期。
		System.out.println("文件大小(字节):" + file1.length());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
        String lastModified = sdf.format(new Date(file1.lastModified()));
        System.out.println("最后修改时间:" + lastModified);
		//(4)将1.txt重命名为2.txt。
        file1.renameTo(new File("d:/dir1/2.txt"));
		//(5)将目录dir1删除。
		File file3 = new File("d:/","dir1");
		recursiveDelete(file3);
	}
//将目录中的文件删除
	public static void recursiveDelete(File file) {
        if (!file.exists())
            return;
        if (file.isDirectory()) {
            for (File f : file.listFiles()) {
                recursiveDelete(f);
            }
        }
        file.delete();
    }
}

2、单词计数:统计文件中单词出现次数,单词间以空格,tab或回车间隔。

import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) throws Exception {
    	//建立文件
    	File file1=new File("d:/2.txt");
		file1.createNewFile();
		file1.canWrite();
		file1.canRead();
		
		//文件内写入内容
		FileWriter file2=new FileWriter("d:/2.txt");
		Scanner cin=new Scanner(System.in);
        String s=cin.nextLine();
        file2.write(s);
		file2.close();
        		
        //读文件
        File file3 = new File("d:/2.txt");
        Scanner input = new Scanner(file3);
        int sum=0;
        while(input.hasNext()) {
        	String neirong = input.next();
        	sum++;
        }
        System.out.println(sum);
    }
}

3、图片还原:文件1.jpg经过如下操作后变为2.jpg,请还原图片1.jpg。

import java.io.FileInputStream;
import java.io.FileOutputStream;
 
public class Main {
    public static void main(String[] args) throws Exception {
        FileInputStream fis = new FileInputStream("d:/2.jpg");
        FileOutputStream fos = new FileOutputStream("d:/1.jpg");
        while (true) {
            int a = fis.read();
            if (a == -1) {
                break;
            } else {
                fos.write(a ^ 100);
            }
        }
        fos.close();
        fis.close();
    }
}

4、学生记录排序

1.txt中保存了若干同学的成绩信息,按如下方式对记录排序再存回到1.txt中。

按班号升序排列,班号相同按成绩降序排列,参考效果:

学号             姓名      班号    数学       语文     英语    总分

20180001    张三         1      100        80        76      256

20180002    李四     1       80         70        90     240

20180004    赵六     2       85        85         90     260

20180003    王五     2       74        70         80     224

20180006    孙八     3      100        90        98     288

20180005    钱七     3      100        70        90     260

import java.io.File;
import java.io.FileWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) throws Exception{
		//建立目录文件并在文件中输入内容
		//Scanner sc = new Scanner(new File("‪d:/1.txt"));
		Scanner sc = new Scanner(new File("d:/1.txt"));
        sc.nextLine();
        List  ss = new ArrayList  ();
        while (sc.hasNext()) {
            Student student = new Student(sc.nextInt(),sc.next(), sc.nextInt(), sc.nextInt(), sc.nextInt(),sc.nextInt());
            ss.add(student);
        }
        sc.close();
        FileWriter file1 = new FileWriter("d:/2.txt");
        file1.write("学号\t\t姓名\t班号\t数学\t语文\t英语\t总分\r\n");
        Collections.sort(ss, new Comparator1());
        for (Student s : ss) {
            file1.write(s.toString());
        }
        file1.close();
        new File("d:/1.txt").delete();
        new File("d:/2.txt").renameTo(new File("d:/1.txt"));
	}
}


class Student
{
	
	int sno,class_num,math,chinese,english,sum;
	String name;
	public Student(int sno, String name,int class_num, int chinese,int math,  int english) {
		super();
		this.sno = sno;
		this.class_num = class_num;
		this.math = math;
		this.chinese = chinese;
		this.english = english;
		this.name = name;
		this.sum = this.math+this.chinese+this.english;
	}
	@Override
	public String toString() {
		try {
			return  sno + "\t" +new String(name.getBytes(),"UTF-8") + "\t"+ class_num + "\t" + chinese + "\t" + math + "\t" + english + "\t" + sum + "\t\n";
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
}


class Comparator1 implements Comparator {
	 
    @Override
    public int compare(Student o1, Student o2) {
        // TODO Auto-generated method stub
        if (o1.sum != o2.sum) {
            return o2.sum - o1.sum;
        } else {
            return o1.class_num - o2.class_num;
        }
    }
 
}

你可能感兴趣的:(Java作业,java)