Java第6次上機

來源:互聯網
上載者:User

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);
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.