4.1 讀入檔案
    
   
  
  
   
    import java.io.*;
    
    // 2000.10.14 by Chen Ching-Han at MIAT     
            Lab.
    
    class ifstream
    
    {
    
    FileInputStream     
            fin;
    
    File fn;
    
    //
    
    ifstream(String     
            filename)
    
    {
    
    fn=new     
            File(filename);
    
    try
    
    fin=new     
            FileInputStream(fn);
    
    }
    
    catch(FileNotFoundException     
            e)
    
    {
    
    System.out.println("File     
            Not Found");
    
    }
    
    }
    
    //
    
    String     
            read()
    
    {
    
    data;
    
    char     
            ch;
    
    String     
            str="";
    
    boolean     
            flag=true;
    
    do
    
    data=(byte)     
            fin.read();
    
    }while((data<=32     
            || data==127) && flag==true);
    
    flag=false;
    
    ch=(char)data;
    
    if(data<=32     
            || data==127) return str;
    
    str=str+ch;
    
    }while(data!=-1);
    
    catch(IOException     
            e)
    
    {
    
    System.out.println("Read     
            File Error");
    
    }
    
    return     
            null;
    
    }
    
    //
    
    void close()
    
    {
    
    try
    
    fin.close();
    
    catch(IOException     
            e)
    
    {
    
    System.out.println("Close     
            File Error");
    
    }
    
    }
    
    //
    
    int     
            rInt()
    
    {
    
    return     
            Integer.valueOf(read()).intValue();
    
    }
    
    //
    
    float rFloat()
    
    {
    
    return     
            Float.valueOf(read()).floatValue();
    
    }
    
    //
    
    double rDouble()
    
    {
    
    return     
            Double.valueOf(read()).doubleValue();
    
    }
    
    }
    
    //-------------------------------------------------------------
   
  
  
   
    
     下載
    
    
     ifstream.java
    
   
  
  
   
    練習6_1
   
  
  
   
    使用記事本輸入4行資料,分別是字串、整數、浮點數、double數,寫一程式將資料讀入,再將其輸出至螢幕。
   
  
  
   
    ifstream     
            in1=new ifstream("data.txt");
    
    n1=in1.rInt();
   
  
  
   或(讓使用者輸入檔名)
  
  
   
    System.out.println("
   
   輸入檔案名稱
   
    : ");
    
    ConsoleReader     
            console = new ConsoleReader(System.in);
    
    filename     
            = console.readLine();
    
    ifstream     
            in1=new ifstream(filename);
    
    n1=in1.rInt();
   
  
  
  
  
  
   
    4.2 寫入檔案
   
  
  
   
    import java.io.*;
    
    import java.lang.*;
    
    // 2000.10.14     
            by Chen Ching-Han at MIAT Lab.
    
    class ofstream
    
    {
    
    File fn;
    
    RandomAccessFile     
            fout;
    
    //
    
    ofstream(String     
            filename)
    
    {
    
    fn=new     
            File(filename);
    
    if(fn.exists())     
            fn.delete();
    
    try
    
    fout=new     
            RandomAccessFile(filename,"rw");
    
    }
    
    catch(IOException     
            e)
    
    {
    
    System.out.println("Create     
            File Error!");
    
    return;
    
    }
    
    //
    
    //input
   
   檔名
   
    ,
   
   如果存在是否刪
   
    RandomfileRandomfile
   
   除
   
    (true or false)
    
    ofstream(String     
            filename,boolean d)
    
    {
    
    fn=new     
            File(filename);
    
    if     
            (d==false && fn.exists())
    
    {
    
    fout=new     
            RandomAccessFile(filename,"rw");
    
    }
    
    catch(FileNotFoundException     
            e)
    
    {
    
    System.out.println("File     
            Not Found");
    
    return;
    
    if(fn.exists())fn.delete();
    
    fout=new     
            RandomAccessFile(filename,"rw");
    
    }
    
    catch(IOException     
            e)
    
    {
    
    System.out.println("Create     
            File Error!");
    
    return;
    
    }
    
    //
    
    void     
            wDouble(double f)
    
    {
    
    String     
            str;
    
    str=Double.toString(f);
    
    wString(str);
    
    }
    
    //
    
    //
   
   將檔案寫入一個浮點數
   
   void wFloat(float     
            f)
   
   {
   
   String     
            str;
   
   str=Float.toString(f);
   
   wString(str);
   
   }
   
   //
   
   //將檔案寫入一個整數
   
   void wInt(int i)
   
   {
   
   String     
            str;
   
   str=Integer.toString(i);
   
   wString(str);
   
   }
   
   //
   
   //將檔案寫入一個跳行
   
   void wln()
   
   {
   
   wByte(13);
   
   wByte(10);
   
   }
   
   //
   
   //將檔案寫入一個
   
    tab
    
    void wTab()
    
    wByte(9);
    
    }
    
    //
    
    //
   
   將檔案寫入一個
   
    String
    
    void wString(String     
            str)
    
    {
    
    try
    
    fout.seek(fout.length());
    
    l=str.length();
    
    for(int     
            j=0;j<l;j++)
    
    {
    
    fout.writeByte((byte)str.charAt(j));
    
    catch(IOException     
            e)
    
    {
    
    System.out.println("Write     
            File Error!");
    
    return;
    
    void wChar(char     
            c)
    
    {
    
    fout.seek(fout.length());
    
    fout.writeChar(c);
    
    catch(IOException     
            e)
    
    {
    
    System.out.println("Write     
            File Error!");
    
    return;
    
    }
    
    //
    
    void     
            wByte(int b)
    
    {
    
    fout.seek(fout.length());
    
    fout.writeByte(b);
    
    catch(IOException     
            e)
    
    {
    
    System.out.println("Write     
            File Error!");
    
    return;
    
    }
    
    void     
            close()
    
    {
    
    fout.close();
    
    catch(IOException     
            e)
    
    {
    
    System.out.println("Close     
            File Error");
    
    }
    
    }
    
    }
    
   
   //-------------------------------------------------------------
  
  
   
    
     下載
    
    
     ofstream.java
    
   
  
  
   
    練習6.2
   
  
  
   
    寫一程式分別將字串、整數、浮點數、double數資料寫入一個檔案,再使用練習6.1程式將其讀出驗證。
   
  
  
   
    ofstream     
            out=new ofstream("title.txt");
   
  
  
   
    out.wString("
   
   義守大學    
             電機系
   
    ");
   
  
  
   
    out.wln();
   
  
  
   
    float pi=3.14159;
   
  
  
   
    out.wFloat(pi);
   
  
  
   
    ..........
   
  
  
   
    本章練習1:
   
  
  
   
    請自行產生一個6列資料(含標題)的純文字檔,名為score.txt,其格式為:第一列為4個字串的標題,第二列以後的每一列的第一項資料為字串、第二、三、四項資料為整數,請將資料從檔案讀入,並輸出到螢幕。
   
  
  
   資料檔範例如下:
  
  
   姓名       國文  英文  數學
  
  
   陳水扁    90       75
  
  
   ......................
  
  
   
    class ex
   
  
  
   
    public static   
            void main (String args[])
   
  
  
   
    ifstream   
            in1=new ifstream("data.txt");
   
  
  
   
    ofstream   
            out=new ofstream("result.txt");
   
  
  
   
    String
   
  
  
   
    
     for(int   
            i=0;i<4;i++)
    
   
  
  
   
    
     str=in1.read();
    
   
   
    
     System.out.print(str+"\t");
    
   
   
    
     out.wString(str);
    
   
   
    
     out.wTab();
    
   
   
    
     out.wln();
    
   
   
    
     str=in1.read();
    
   
   
    
     System.out.print("\n"+str+"\t");
    
   
   
    
     out.wString(str);
    
   
   
    
     out.wTab();
    
   
   []x = new int[3];
  
  
  
  
   
    for(int   
            i=0;i<3;i++)
   
  
  
   
    x[i]=in1.rInt();
   
  
  
   
    System.out.print(x[i]+"\t");
   
  
  
   
    out.wInt(x[i]);
   
  
  
   
    out.wTab();
   
  
  
   本章練習2:
  
  
   download
   
    
     stock.txt
    
   
  
  
   練習時間序列資料處理。
  
  
   
    class ex
   
  
  
   
    public static   
            void main (String args[])
   
  
  
   
    ifstream   
            in1=new ifstream("stock.txt");
   
  
  
   
    ofstream   
            out=new ofstream("result.txt");
   
  
  
   
    String   
            [] str = new String[319];
   
  
  
   
    float   
            [] x =new float[319];
   
  
  
   
    for(int   
            i=0;i<319;i++)
   
  
  
   
    str[i]=in1.read();
   
  
  
   
    x[i]=in1.rFloat();
   
  
  
   
    //        System.out.print(str[i]+"\t"+x[i]+"\n");
   
  
  
   
    float   
            max,min;
   
  
  
   
    max=x[0];
   
  
  
   
    min=x[0];
   
  
  
   
    for(int   
            i=0;i<319;i++)
   
  
  
   
    if(x[i]>max)max=x[i];
   
  
  
   
    if(x[i]<min)min=x[i];
   
  
  
   
    System.out.print("max="+max+"\nmin="+min);
   
  
  
   
    for(int   
            i=0;i<319;i++)
   
  
  
   
    x[i]=(x[i]-min)/(max-min);
   
  
  
   
    out.wString(str[i]);
   
  
  
   
    out.wTab();
   
  
  
   
    out.wFloat(x[i]);
   
  
  
   
    out.wln();
   
  
  
   本章練習3:
  
  
   請使用相同的資料檔,計算時間序列的5點的平均濾波、微分和積分。例
  
  
   5點的平均濾波:
  
  
   float [] y;
   
   y = new float[319];
   
   float [] filter={0.2f,0.2f,0.2f,0.2f,0.2f};
   
   for(int i=0;i<2;i++)y[i]=x[i];
   
   for(int i=317;i<319;i++)y[i]=x[i];
   
   for(int i=2;i<319-2;i++)
   
   y[i]=0;
   
   for(int j=0;j<5;j++)
   
   y[i]=y[i]+x[i+j-2]*filter[j];