Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 2020/4/24(金)~25(土)
- import java.io.*;
- public class TestFileInput {
- public static void main(String[] args) throws Exception {
- System.out.println("\n行列を入力します");
- double[][] matrix;
- if (args.length == 0) {
- System.out.println("ファイル名の指定がありません");
- return;
- } else if (args[0].equals("-i") && args.length == 2) {
- matrix = readFile(args[1]);
- } else {
- return;
- }
- }
- public static double[][] readFile(String fileName) throws Exception {
- FileReader fr = new FileReader(fileName);
- BufferedReader br = new BufferedReader(fr);
- String line = null;
- int lineCount = 0;
- String[] lineArray = null;
- while((line = br.readLine()) != null) {
- if (lineCount == 0) {
- lineArray = line.split("[ ]");
- }
- lineCount++;
- }
- br.close();
- double[][] matrix = new double[lineArray.length][lineCount];
- fr = new FileReader(fileName);
- br = new BufferedReader(fr);
- line = null;
- int lineNum = 0;
- String[][] lineTable = new String[lineArray.length][lineCount];
- while((line = br.readLine()) != null) {
- lineTable[lineNum] = line.split("[ ]");
- lineNum++;
- }
- for (int i = 0; i < lineArray.length; i++) {
- for (int j = 0; j < lineCount; j++) {
- matrix[i][j] = Double.parseDouble(lineTable[i][j]);
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- br.close();
- return matrix;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement