| 1 |
package fileIO; |
| 2 |
|
| 3 |
|
| 4 |
public class ArrayFile extends TextFile{ |
| 5 |
|
| 6 |
public static void main(String[] args){ |
| 7 |
|
| 8 |
try { |
| 9 |
|
| 10 |
String name=args[0]; |
| 11 |
|
| 12 |
ArrayFile mat=new ArrayFile(name); |
| 13 |
|
| 14 |
String s=null; |
| 15 |
|
| 16 |
for(s=mat.readLine(); s!=null; s=mat.readLine()){ |
| 17 |
System.out.println(s); |
| 18 |
} |
| 19 |
} catch (Exception e) { |
| 20 |
throw new RuntimeException(e); |
| 21 |
} |
| 22 |
|
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
public ArrayFile(String name){super(name, false);} |
| 27 |
|
| 28 |
@Override |
| 29 |
public String nextLine(){ |
| 30 |
String line=readLine(); |
| 31 |
char c=line.charAt(0); |
| 32 |
|
| 33 |
while(line!=null && c!='{' && c!='/'){ |
| 34 |
line=readLine(); |
| 35 |
c=line.charAt(0); |
| 36 |
} |
| 37 |
return line; |
| 38 |
} |
| 39 |
|
| 40 |
public float[] nextArray(){ |
| 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 |
int length=Integer.parseInt(line); |
| 54 |
|
| 55 |
|
| 56 |
float[] grid=new float[length]; |
| 57 |
|
| 58 |
line=nextLine(); |
| 59 |
assert(line.startsWith("{")); |
| 60 |
if(line.endsWith(",")){line=line.substring(0, line.length()-1);} |
| 61 |
assert(line.endsWith("}")); |
| 62 |
line=line.replace("{", "").replace("}", "").replace(" ", ""); |
| 63 |
split=line.split(","); |
| 64 |
assert(split.length==length); |
| 65 |
for(int i=0; i<split.length; i++){ |
| 66 |
grid[i]=Float.parseFloat(split[i]); |
| 67 |
} |
| 68 |
|
| 69 |
return grid; |
| 70 |
} |
| 71 |
|
| 72 |
} |