| 1 |
package fileIO; |
| 2 |
import dna.Matrix; |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
public class MatrixFile extends TextFile{ |
| 7 |
|
| 8 |
public static void main(String[] args){ |
| 9 |
|
| 10 |
try { |
| 11 |
|
| 12 |
String name=args[0]; |
| 13 |
|
| 14 |
MatrixFile mat=new MatrixFile(name); |
| 15 |
|
| 16 |
String s=null; |
| 17 |
|
| 18 |
for(s=mat.readLine(); s!=null; s=mat.readLine()){ |
| 19 |
System.out.println(s); |
| 20 |
} |
| 21 |
} catch (Exception e) { |
| 22 |
throw new RuntimeException(e); |
| 23 |
} |
| 24 |
|
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
public MatrixFile(String name){super(name, false);} |
| 29 |
|
| 30 |
@Override |
| 31 |
public String nextLine(){ |
| 32 |
String line=readLine(); |
| 33 |
|
| 34 |
while(line!=null && line.charAt(0)!='{' && line.charAt(0)!='/'){ |
| 35 |
line=readLine(); |
| 36 |
} |
| 37 |
return line; |
| 38 |
} |
| 39 |
|
| 40 |
public Matrix nextMatrix(){ |
| 41 |
String line; |
| 42 |
String[] split; |
| 43 |
|
| 44 |
line=nextLine(); |
| 45 |
if(line==null || line.startsWith("//end")){return null;} |
| 46 |
|
| 47 |
assert(line.startsWith("//name: ")) : line; |
| 48 |
String name=line.replace("//name: ","").trim(); |
| 49 |
|
| 50 |
line=nextLine(); |
| 51 |
assert(line.startsWith("//size: ")) : line; |
| 52 |
line=line.replace("//size: ",""); |
| 53 |
split=line.split("x"); |
| 54 |
int length=Integer.parseInt(split[0]); |
| 55 |
int width=Integer.parseInt(split[1]); |
| 56 |
|
| 57 |
line=nextLine(); |
| 58 |
assert(line.startsWith("//prefix: ")) : line; |
| 59 |
line=line.replace("//prefix: ",""); |
| 60 |
int prefix=Integer.parseInt(line); |
| 61 |
|
| 62 |
line=nextLine(); |
| 63 |
assert(line.startsWith("//count: ")) : line; |
| 64 |
line=line.replace("//count: ",""); |
| 65 |
int count=Integer.parseInt(line); |
| 66 |
|
| 67 |
|
| 68 |
float[][] grid=new float[length][width]; |
| 69 |
for(int i=0; i<length; i++){ |
| 70 |
line=nextLine(); |
| 71 |
|
| 72 |
while(line.startsWith("//")){line=nextLine();} |
| 73 |
|
| 74 |
assert(line.startsWith("{")); |
| 75 |
if(line.endsWith(",")){line=line.substring(0, line.length()-1);} |
| 76 |
assert(line.endsWith("}")); |
| 77 |
line=line.replace("{", "").replace("}", "").replace(" ", ""); |
| 78 |
split=line.split(","); |
| 79 |
assert(split.length==width); |
| 80 |
for(int j=0; j<split.length; j++){ |
| 81 |
grid[i][j]=Float.parseFloat(split[j]); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
return new Matrix(grid, prefix, name); |
| 86 |
} |
| 87 |
|
| 88 |
} |