【发布时间】:2015-12-06 22:53:48
【问题描述】:
我在使用分隔符和解析之前读入了文本文件数据,但我似乎不知道如何处理这个文件。第一行是多边形形状的数量,然后每个 x/y(用逗号分隔)点用 :'s 分隔,然后是 |分隔 rgb 颜色值。在没有用新行分隔数据之前,我没有使用过任何东西,这真的让我很困惑。我基本上需要一个循环,它将获取一个包含 x 值、y 值的 int 数组,然后为每个 R、G 和 B 获取三个单独的 int。在循环再次迭代之前,将数据发送到新多边形的构造函数中。
5 382,100:352,135:332,172:327,214:340,277:372,316:421,334:493,346:539,325:567,301:589,254:602,202:593,154:555,126:5007,99:4120,91|1| 409,152:402,170:411,185:431,185:434,167:425,153|51,51,255 502,180:517,183:527,171:525,158:506,152:496,163|0,51,255 381,223:389,247:408,271:440,292:481,296:511,286:538,251:537,230:518,247:502,265:467,273:437,266:410,250|255,0,102 366,375:355,329:338,267:328,208:333,175:352,137:382,104:436,92:505,100:593,156:602,204:590,249:572,295:559,356:606,369:628,333:607,345:581,328:593,295:607,247:613,203:597,142:554,111:486, 83:428,81:384,87:352,108:329,140:318,175:315,213:316,243:324,288:339,332:347,353:320,343:339,367|0,0,0
我还没有走得太远,如果有任何建议,我将不胜感激。
int numShapes, counter=0;
try
{
File myfile = new File (fileToOpen);
Scanner textScan = new Scanner(myfile); // reads in date from text file
numShapes = Integer.parseInt(textScan.nextLine()); //read in first line (number of shapes) and parse to int
while (textScan.hasNextLine()) //
{
//read in all the points until you hit the | that starts color info
textScan.useDelimiter("|");
while(textScan.hasNext())
{
}
//add points to shape
// in loop - shapeList.get(counter).addPoint();
} // end of loop to read in shape text file contents
textScan.close(); //closes shape file
}
catch (Exception e)
{
JOptionPane.showMessageDialog(theFrame, "I/O Problem - File not Saved");
}
这是我将用于多边形的构造函数。
MyPoly(int [] xpts, int [] ypts, int npts, Color col)
【问题讨论】:
-
您希望您的文件有多大?
-
最后,您是否在 RGB 值之后有换行符?
-
文本文件示例大约是最大的。 rgb 值后不一定有换行符,具体取决于列出的每个多边形中的点数。
标签: java parsing text-files delimiter polygons