Java 如何拆分每行的最后2个字符串以重复替换它们

Java 如何拆分每行的最后2个字符串以重复替换它们,java,split,Java,Split,我试图从存储坐标值的数据库写入文件。 我要保存到的文件是SVG文件,我不知道如何替换moveTo坐标和curveTo坐标?另外,如何在每行中重复C命令?数据以字符串的形式存储在数据库中 355.22 632.0771 355.574 631.338 315.228 632.077 455.22 632.0771 355.574 631.338 335.228 632.077 555.22 632.0771 355.574 631.338 375.228 632.077 655.22 232.07

我试图从存储坐标值的数据库写入文件。 我要保存到的文件是SVG文件,我不知道如何替换
moveTo
坐标和
curveTo
坐标?另外,如何在每行中重复C命令?数据以字符串的形式存储在数据库中

355.22 632.0771 355.574 631.338 315.228 632.077
455.22 632.0771 355.574 631.338 335.228 632.077
555.22 632.0771 355.574 631.338 375.228 632.077
655.22 232.0771 355.574 631.338 385.228 632.077
755.22 332.0771 355.574 631.338 365.228 632.077
255.22 432.0771 355.574 631.338 395.228 632.077
155.22 532.0771 355.574 631.338 355.228 632.077

最后两个值用于移动变量,即:
move=“355.228 632.077”

每条线被视为一条曲线,曲线变量即:
curve=“355.22 632.0771 355.574 631.338 315.228 632.077”在我的示例中有7条曲线。
例如:

public class WriteToSVG {

private Formatter f;

public void openFile(){
    try{
        //To Open & create the a File
        f=new Formatter("C:\\demo.svg");
    }
    catch(Exception e){
        System.out.println("Error occured");
    }
}

public void addRecords(){
    try{
        //Add Data to the File
        String str="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
                "<!-- Generator: Adobe Illustrator 14.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 43363)  -->\n" +
                "\n" +
                .....";
        f.format(str);
        String space=" ";
        String fill="003399";
        String fill_opacity="1";
        String strock="none";
        String move = "";
        String[] moveSplit = move.split("\\rs");
        String curve="1022.9594,426.89007 1022.7136,426.30999 1022.5051,425.84922";
        String id="path4044-3";
        String nodeType="cccccccccccccccccc"; 
        f.format("<path%s" +"style="+"\"fill:#%s;fill-opacity:%s;stroke:%s\""+"\nd=",space,fill,fill_opacity,strock);
        f.format("\"M%s",move);//moveTo variable
        f.format( "C%s",curve);//curveTo variable
        f.format("z\""+"\nid=\"%s\""+"\ninkscape:connector-curvature=\"0\""+ "\n sodipodi:nodetypes=\"%s\""+"/>",space,fill,fill_opacity,strock,id,nodeType);
        f.format("</svg>");
    System.out.println("Data added to the File successfully");
      }
    catch(Exception e){
        System.out.println("Error occured");
    }
}
public void closeFile(){
    f.close();
}
public static void main(String[] args) {
    WriteToSVG w=new WriteToSVG();
    w.openFile();
    w.addRecords();
    w.closeFile();
}}
公共类WriteToSVG{
专用格式化程序f;
公共void openFile(){
试一试{
//打开并创建文件的步骤
f=新格式化程序(“C:\\demo.svg”);
}
捕获(例外e){
System.out.println(“发生错误”);
}
}
公共档案({
试一试{
//将数据添加到文件中
字符串str=“\n”+
“\n”+
“\n”+
.....";
f、 格式(str);
字符串空格=”;
字符串填充=“003399”;
字符串填充_opacity=“1”;
字符串strock=“无”;
字符串move=“”;
字符串[]moveSplit=move.split(\\rs”);
字符串曲线=“1022.9594426.89007 1022.7136426.30991022.5051425.84922”;
String id=“path4044-3”;
字符串nodeType=“cccc”;
f、 格式(“”,空格,填充,填充不透明度,strock,id,节点类型);
f、 格式(“”);
System.out.println(“数据成功添加到文件中”);
}
捕获(例外e){
System.out.println(“发生错误”);
}
}
公共文件(){
f、 close();
}
公共静态void main(字符串[]args){
WriteToSVG w=新的WriteToSVG();
w、 openFile();
w、 addRecords();
w、 closeFile();
}}

如何根据以上各点使用拆分,其中我要使用每条线的最后2个点?

您可以使用以下各项:

final String[] lines = {
    "355.22 632.0771 355.574 631.338 315.228 632.077",
    "455.22 632.0771 355.574 631.338 335.228 632.077",
    "555.22 632.0771 355.574 631.338 375.228 632.077",
    "655.22 232.0771 355.574 631.338 385.228 632.077",
    "755.22 332.0771 355.574 631.338 365.228 632.077",
    "255.22 432.0771 355.574 631.338 395.228 632.077",
    "155.22 532.0771 355.574 631.338 355.228 632.077"
};

for(final String line : lines) {
    final String[] parts = line.split(" (?=[\\d.]* [\\d.]*$)");
    final String[] curves = parts[0].split(" ");
    final String[] moves = parts[1].split(" ");

    // here, curves contains your first four numbers and moves contains the two last ones
}

工作原理:

  • 对于每一行,我们将内容分为两部分
  • 对于第一部分,在空格上拆分其内容将给出曲线坐标列表
  • 对于第二部分,在空格上拆分其内容将给出移动坐标列表
例如,考虑到行
“355.22 632.0771 355.574 631.338 315.228 632.077”

我希望这有帮助;)

line = "355.22 632.0771 355.574 631.338 315.228 632.077";
// splitting here                      ^
parts = {"355.22 632.0771 355.574 631.338", "315.228 632.077"};

parts[0] = "355.22 632.0771 355.574 631.338";
// splitting      ^        ^       ^
curves = {"355.22", "632.0771", "355.574", "631.338"};

parts[1] = "315.228 632.077";
// splitting       ^
moves = {"315.228", "632.077"};