輕鬆掌握Java按行讀取數據內容,並在該行末尾加上總成績並寫到新的文件中(包含正則表達式的求法)

現有如下格式的成績單(文本格式)grate.txt:

姓名:馬超,數學101分,英語120分,理綜280
姓名:韓信,數學120分,英語130分,理綜300
姓名:猴子,數學100分,英語145分,理綜290

要求:
按行讀入取成績單,並在該行的最尾部加上該同學的總成績,然後再將該行寫入到一個名字爲grates.txt的文件中

代碼實現

方法①:

package lei;

import java.io.*;

public class dahfcbdcj {
      public static void main(String[] args) {
       File file=new File("c:\\javas\\grate.txt");
          File file1=new File("c:\\javas\\kk\\grates.txt");
          Students s=new Students();
          s.read(file,file1);
   }
}

class Students{
    String str=null;
    BufferedReader r;
    BufferedWriter w;
    public void read(File file,File file1){
        try{
            r=new BufferedReader(new FileReader(file));
            w=new BufferedWriter(new FileWriter(file1));
            while((str=r.readLine())!=null){
                double j=0,k=0;
                char[] a=str.toCharArray();
                for(int i=0;i<str.length();i++){
                    if(a[i]>='0'&&a[i]<='9'){//這裏的0——9表示的是ASSIC碼錶中的,0的ASSIC碼值爲48
                        j=(double)(a[i]-48);
                        i++;
                        if(a[i]>='0'&&a[i]<='9'){
                            j=j*10+(double)(a[i]-48);
                        }
                        i++;
                        if(a[i]>='0'&&a[i]<='9'){
                            j=j*10+(double)(a[i]-48);
                        }
                        k+=j;
                    }else{
                        j=0;
                    }
            }
                String s1=str+"-------------總分"+k+"分";
                System.out.println(s1);
                w.write(s1);
                w.newLine();
            }
            w.close();
            r.close();
        }catch(IOException e){

        }
    }
}
方法②:

public class dahfcbdcj2 {
      public static void main(String[] args) {
       File file=new File("c:\\javas\\grate.txt");
          File file1=new File("c:\\javas\\kk\\grates.txt");
          Students2 s=new Students2();
          s.read(file,file1);
   }
}


class Students2{
    String str=null;
    BufferedReader r;
    BufferedWriter w;
    public void read(File file,File file1){
        try{
            r=new BufferedReader(new FileReader(file));
            w=new BufferedWriter(new FileWriter(file1));
            while((str=r.readLine())!=null){
                double r=0;
                char[] a=str.toCharArray();
                for(int i=0;i<str.length();i++){
                 double j = 0 ;
                 while(a[i]>='0'&&a[i]<='9') {
                  j=j*10+(double)(a[i]-48);
                        i++;
                 }
                 r += j ;
                }
                String s1=str+"-------------總分"+r+"分";
                System.out.println(s1);
                w.write(s1);
                w.newLine();
            }
            w.close();
            r.close();
        }catch(IOException e){

        }
    }
}
方法三:使用正則表達式進行求法:

public class dafgsfhfjkdhfuksd {
  public static void main(String[] args) throws IOException {
   File file=new File("c:\\javas\\grate.txt");
      File file1=new File("c:\\javas\\kk\\grates.txt");
      BufferedReader br = new BufferedReader( new FileReader(file));
      BufferedWriter bw = new BufferedWriter( new FileWriter(file1));
      String str = null ;
      while ( ( str = br.readLine() ) != null ) {
       double scores = StudentScore.totalScore(str);
       str = str + "總成績:" + scores ;
       System.out.println(str);
       bw.write(str);
       bw.newLine();
      }
      br.close();
      bw.close();
  }
}
class StudentScore{
 public static double totalScore(String s) {
  String regex = "[^0123456789]";//正則表達式,[^0123456789]相當於[^0-9]
  String digitMess = s.replaceAll(regex, "*");
  StringTokenizer  st  =  new  StringTokenizer(digitMess,"*");
  double scores = 0;
  while(st.hasMoreTokens()) {
   double score = Double.parseDouble(st.nextToken());
   scores += score;
  }
  return scores;
 }
}
說明:

 StringTokenizer 位於java.util.StringTokenizer包中
string tokenizer 類允許應用程序將字符串分解爲標記。tokenization 方法比 StreamTokenizer 類所使用的方法更簡單。
StringTokenizer 方法不區分標識符、數和帶引號的字符串,它們也不識別並跳過註釋。 

hasMoreTokens()  測試此 tokenizer 的字符串中是否還有更多的可用標記。----- boolean

nextToken()  返回此 string tokenizer 的下一個標記。------String

想要了解該方法的更多內容可以自行查詢API文檔

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章