在java中读取文本文件数据以创建多边形

在java中读取文本文件数据以创建多边形,java,parsing,text-files,delimiter,polygons,Java,Parsing,Text Files,Delimiter,Polygons,在使用分隔符和解析之前,我已经读入了文本文件数据,但我似乎不知道如何处理这个文件。第一行是多边形形状的数量,然后每个x/y(用逗号分隔)点用“:”分隔,然后a |分隔rgb颜色值。我以前从未处理过数据之间没有新行分隔的情况,这让我很困惑。我基本上需要一个循环,它将得到一个包含x值、y值的int数组,然后每个R、G和B都有三个独立的int。在循环再次迭代之前,我会将数据发送到一个新多边形的构造函数中 MyPoly(int [] xpts, int [] ypts, int npts, Color

在使用分隔符和解析之前,我已经读入了文本文件数据,但我似乎不知道如何处理这个文件。第一行是多边形形状的数量,然后每个x/y(用逗号分隔)点用“:”分隔,然后a |分隔rgb颜色值。我以前从未处理过数据之间没有新行分隔的情况,这让我很困惑。我基本上需要一个循环,它将得到一个包含x值、y值的int数组,然后每个R、G和B都有三个独立的int。在循环再次迭代之前,我会将数据发送到一个新多边形的构造函数中

MyPoly(int [] xpts, int [] ypts, int npts, Color col)
五, 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:507,99:442,91|102,102,0 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 366375:355329:338267:328208:333175:352137:382104:436,92:505100:593156:602204:590249:572295:559356:606369:628333:607345:581328:593295:607247:613203:597142:554111:486,83:428,81:384,87:352108:329140:318175:213:316243:3284:339332:3453:320343:339367

我还没有走得太远,如果有任何建议,我将不胜感激

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)

如果不能保证总是有换行符,那么唯一可行的选择是将整个文件加载到字符串中,然后使用StringTokenizer逐段解析它

然而,考虑到示例文本中似乎有新行,我很难相信没有新行。因此,如果有机会可以保证换行,那么您还有另一个更简单的选择:

逐行加载文件,并使用
String.split()
将每行拆分为其组成部分。例如,首先在
|
上拆分直线以从RGB中分离点,然后在
上拆分点以分离每个点,然后在
上拆分每个点以获得坐标。

像这样读取每一行(行数无用)

然后像这样对待每一行:

分割点|颜色

拆分点1:点2:

和分割点和颜色

// example with a line
String a_line="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";

// SPLIT POINTS | COLOR
String parts[]=a_line.split("\\|");

// Should be 2
System.out.println("PARTS:"+parts.length);

if (parts.length!=2) {} // STOP ?

System.out.println("PART LEFT:"+parts[0]);

System.out.println("PART RIGHT:"+parts[1]);

// SPLIT POINTS
String points[]=parts[0].split("\\:");

int numpoints=points.length;
System.out.println("POINTS:"+numpoints);

for (int k=0;k<numpoints;k++)
{
    // ONE POINT
    String one_point[]=points[k].split("\\,");
    //Should be 2
    if (one_point.length!=2) {continue;} // STOP ?

    int point_X=Integer.parseInt(one_point[0]);
    int point_Y=Integer.parseInt(one_point[1]);
    System.out.println("X="+point_X+" Y="+point_Y);

    // YOU MUST STORE THAT !
}

// SPLIT COLOR
String colors[]=parts[1].split("\\,");
//Should be 3
System.out.println("COLORS:"+colors.length);
if (colors.length!=3) {} // STOP ?

int color_R=Integer.parseInt(colors[0]);
int color_G=Integer.parseInt(colors[1]);
int color_B=Integer.parseInt(colors[2]);
System.out.println("R="+color_R+" G="+color_G+" B="+color_B);
//带行的示例
字符串a_line=“366375:355329:338267:328208:333175:352137:382104:436,92:505100:593156:602204:590249:572295:559356:606369:628333:607345:581328:593295:607247:613203:597142:554111:486,83:428,81:384,87:352108:329140:318175:213:316243:288:34733:337332:1240,0”;
//分割点|颜色
字符串部分[]=一行分割(“\\\\”);
//应该是2
System.out.println(“零件:+零件长度”);
如果(parts.length!=2){}//停止?
System.out.println(“部分左侧:“+parts[0]);
System.out.println(“部件权利:“+部件[1]);
//分割点
字符串点[]=部分[0]。拆分(“\\:”);
int numpoints=points.length;
System.out.println(“点:“+numpoints”);

对于(int k=0;kHow large),您希望您的文件是大的吗?最后,您是否在RGB值后有换行符?文本文件示例大约是最大的。RGB值后不一定有换行符,这取决于列出的每个多边形中有多少个点。