0

my goal is to copy the identical content of a file, replace some words and write the content in a new file.

I don't understand the problem, it takes the content correctly except for the line breaks. It also recognizes multiple consecutive white spaces but does not take the new line. Here the code i wrote for it:

public class FileDefinition {

String result = "";

String pathxml = pathxmlin;

String pathconf = pathfileconf;

//func to be recalled in the main for the different files
public String getPropValues() throws IOException {
    try {
        Properties prop = new Properties();         
        String pathconf = this.pathconf;
        String pathxml = this.pathxml;
        
        //Read file conf
        File inputFile = new File(pathconf);
        InputStream is = new FileInputStream(inputFile);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        //load the buffered file
        prop.load(br);


        // get the property value and print it out
        String name = prop.getProperty("name");
        String loc = prop.getProperty("location");
        String id = prop.getProperty("PropID");
        
        //Read scxml file to get the format
        FileReader reader = new FileReader(pathxml);
            
        //research fields to be changed
        String searchname = "RotaionToInside";
        String searchlocation = "location";
        String searchpropid = "PropOfJobID";
            
        String newString;
        StringBuffer str = new StringBuffer();;
    
        BufferedReader rb = new BufferedReader(reader);
        while ((newString = rb.readLine()) != null){
        str.append(newString.replaceAll(searchlocation, loc));
        str.append(newString.replaceAll(searchpropid, id));
        str.append(newString.replaceAll(searchname, name));             
        }

        rb.close();
        
        String pathwriter = pathxml + name + ".scxml";
        
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File(pathscxmlout)));
        bw.write(str.toString());        
        //flush the stream
        bw.flush();      
        //close the stream
        bw.close();
            
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }     
    
    return result;
}   
}
Webpe
  • 15
  • 3
  • 1
    You're writing the text to the new file but are not writing the line breaks. *You* need to do this if you want to keep them. – Hovercraft Full Of Eels Aug 29 '21 at 16:57
  • Get your OS's line separator via `String lineSeparator = System.getProperty("line.separator");`, and then you can do `str.append(lineSeparator);` within your while loop. Incidentally, you look to be writing your line 3 times(?). – Hovercraft Full Of Eels Aug 29 '21 at 17:01
  • thanks for your answer, the proposed method works =). about the 3 append I hadn't noticed since the file was formatted badly, now having solved the new line I start trying to fix this. – Webpe Aug 29 '21 at 17:28
  • `str.append(newString.replaceAll(searchlocation, loc).replaceAll(searchpropid, id).replaceAll(searchname, name));` – Hovercraft Full Of Eels Aug 29 '21 at 18:11

0 Answers0