Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class CSVFile {
  4. public static void main(String[] args) {
  5. FileReader fr = null;
  6. StringBuilder sb = new StringBuilder();
  7. try {
  8. fr = new FileReader("File_5ch.csv");
  9. int charaInt = fr.read();
  10. while(charaInt != -1) {
  11. char chara = (char) charaInt;
  12. sb.append(chara);
  13. charaInt = fr.read();
  14. }
  15. } catch(IOException ex) {
  16. System.out.println("ファイル読み込みエラーです");
  17. } finally {
  18. try {
  19. fr.close();
  20. } catch(IOException ex2) {
  21. System.out.println("ファイルクローズエラーです");
  22. }
  23. }
  24. String files = sb.toString();
  25. System.out.println(files);
  26.  
  27. // data_001.jpg,150
  28. // data_002.jpg,899
  29.  
  30. // ???.matches("[,]");
  31. // ???.matches("[\n]");
  32.  
  33. String[] filesArray = files.split("[\n]");
  34. // for (int i = 0; i < filesArray.length; i++) {
  35. // System.out.println(filesArray[i]);
  36. // }
  37. String[] capaArray = new String[filesArray.length];
  38. for (int i = 0; i < filesArray.length; i++) {
  39. capaArray[i] = filesArray[i].substring(13, 16);
  40. }
  41. for (int i = 0; i < capaArray.length; i++ ) {
  42. System.out.println(capaArray[i]);
  43. }
  44.  
  45. int capaTotal = 0;
  46. for (int i = 0; i < capaArray.length; i++) {
  47. capaTotal += Integer.parseInt(capaArray[i]);
  48. }
  49. System.out.println("合計は" + capaTotal + "です");
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement