Java第6次上机

本文通过几个示例介绍了Java中的I/O操作,包括文件读写、数据输入输出流的使用,以及如何处理学生数据的序列化和反序列化。

Java基本的API中,I/O的基本应用,包括:File类,InputStream,OutputStream,FileInputStream类,FileOutputStream类,DateInputStraeam,DataOutputStream,Reader,Writer,InputStreamReader,OutputStreamWriter,BufferedRead,BufferedWriter等。
-----------------------------------------------------------------------------------------------------------------------------------------
package alone;
import java.io.*;
import java.util.Date;
public class Alone13_1 {
    public static void main(String[] args) throws IOException {
        File f=new File(new BufferedReader(
                  new InputStreamReader(System.in)).readLine());
        File []fs=f.listFiles();
        for(int i=0;i<f.length();i++)
        {
            if(fs[i].isDirectory())
                System.out.println("目录:"+fs[i]);
            else
                {
                System.out.print("文件:"+fs[i]);
                System.out.print(" 大小:"+fs[i].length()/1024+"Kbyte ");
                if(fs[i].isHidden())System.out.print("隐藏属性:是 ");
                else System.out.print("隐藏属性:是 ");
                System.out.print("日期:"+
                        new Date(fs[i].lastModified()).toLocaleString()+"/n");
                }
        }
    }
}

----------------------------------------------------------------------------------------------------------------------------------------------
package alone;
import java.io.*;
public class Alone13_2 {
    public static void main(String[] args) throws Exception, IOException {
        String [][]stud={
                {"洪吉通","80","75","65","50"},
                {"徐光豪","90","100","100","100"},
                {"林顺喜","60","70","55","75"},
                {"李之梅","60","80","75","80"},
                {"洪顺照","80","70","90","85"},
                {"朴顺吉","100","80","90","85"}       
        };
        String fp="D://stud.data";
        DataOutputStream dos=new DataOutputStream(new FileOutputStream(new File(fp)));
        for(int i=0;i<stud.length;i++)
        {
            dos.writeUTF(stud[i][0]);
            //System.out.print(stud[i][0]);
            for(int k=1;k<=4;k++)
                dos.writeInt(Integer.parseInt(stud[i][k]));
                //System.out.print(stud[i][k]);
            //System.out.print("/n");
               
        }
        dos.close();
    }
}
----------------------------------------------------------------------------------------------------------------------------------------------
package alone;
import java.io.*;
public class Alone13_3 {
    public static void main(String[] args) throws IOException {
        Student[] s =new Student[6];
        String fp="D://stud.data";
        DataInputStream dos=new DataInputStream(
                new FileInputStream(new File(fp)));
        for(int i=0;i<6;i++)
        {s[i]=new Student(dos.readUTF(),dos.readInt(),
                    dos.readInt(),dos.readInt(),dos.readInt());
            s[i].get();       
        }       
        dos.close();
    }
}
class Student{
  String name;
  int kor,eng,mat,sci;
  Student(){}
  Student(String name,int kor,int eng,int mat,int sci)
  {
      this.name=name; this.kor=kor; this.eng=eng;
      this.mat=mat; this.sci=sci;     
  }
  public void get()
  {System.out.println(name+" "+kor+" "+eng+" "+mat+" "+sci);}
 
  public void set(String name,int kor,int eng,int mat,int sci)
  {      this.name=name; this.kor=kor;this.eng=eng;
      this.mat=mat;this.sci=sci;     
  }
}
----------------------------------------------------------------------------------------------------------------------------------------------
package alone;
import java.io.*;
public class Alone13_4 {   
    public static void main(String[] args) throws Exception {
        int n1,n2,n3;
        System.out.println("第一个整数?");
        n1=Integer.parseInt(new BufferedReader(
                new InputStreamReader(System.in)).readLine());
        System.out.println("第二个整数?");
        n2=Integer.parseInt(new BufferedReader(
                new InputStreamReader(System.in)).readLine());
        System.out.println("第三个整数?");
        n3=Integer.parseInt(new BufferedReader(
                new InputStreamReader(System.in)).readLine());
        System.out.println("结果");
        System.out.println("合计:"+(n1+n2+n3));
        System.out.println("平均:"+(n1+n2+n3)/3);
    }
}

关于《Java 2实用教程第六版》的相关上机练习和实验指导,虽然目前并未直接提及具体的题目及其解答[^3],但从已有的资料来看,《Java 2实用教程》系列书籍通常会提供丰富的实践内容来帮助学习者巩固所学知识点。 一般情况下,这类教材的每一章都会附带相应的编程案例以及课后习题。这些习题可能涉及基础语法的应用、面向对象的设计思路训练或是更复杂的多线程处理等内容。例如,在讲解线程部分时可能会有如下类型的练习: ### 编写一个多线程程序 要求学生能够独立完成一个简单的多线程应用程序,其中至少包含两个并发执行的任务,并通过`Thread.currentThread().getName()`方法打印当前运行线程的名字以便观察不同线程的行为差异[^1]。 另外还有一种常见的练习形式是结合实际应用场景进行功能开发,比如利用`Scanner`类读取外部数据源(如文本文件),并对其进行必要的分析操作[^2]。下面给出一段示范代码用于展示如何在一个新启动的子线程里借助`Scanner`逐行扫描指定路径下的TXT文档内容: ```java import java.io.File; import java.util.Scanner; public class FileWordReader extends Thread { private String filePath; public FileWordReader(String path){ this.filePath=path; } @Override public void run() { try{ Scanner scanner=new Scanner(new File(filePath)); while(scanner.hasNextLine()){ System.out.println(Thread.currentThread().getName()+" reads:"+scanner.nextLine()); } scanner.close(); }catch(Exception e){ System.err.println(e.getMessage()); } } public static void main(String[] args)throws InterruptedException{ FileWordReader reader=new FileWordReader("example.txt"); reader.start(); // 启动线程 reader.join(); // 主线程等待reader结束 } } ``` 尽管上述例子来源于较旧版本的教学材料,但对于最新一版的具体章节配套作业仍需查阅正版纸质书或者官方授权发布的电子资源获取最权威的信息。如果手头已有该书但找不到对应答案,则建议尝试联系出版社客服咨询是否有额外提供的教师用书或解决方案手册出售;同时也可考虑加入一些专注于计算机科学教育的学习交流群组向其他同学讨教经验心得。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值