Results 1 to 11 of 11
  1. #1
    dmk.3678 is offline Member
    Join Date
    Jun 2015
    Posts
    8
    Rep Power
    0

    Default Directory Path written in wrong format in config.properties file

    Hi,

    I have build a swing application which take 2 Directory paths, one is File Input Directory path and another is File Output Directory path with help of JFileChooser. and then i am setting this absolutePath to one JTextField. Below is the code.

    JButton button = new JButton("Browse");
    button.setIcon(new ImageIcon(GuiApp.class.getResource("/images/Browse.png")));
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.DIRE CTORIES_ONLY);
    int returnVal=fileChooser.showOpenDialog(GuiApp.this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    File files = fileChooser.getSelectedFile();
    if(files.isDirectory())
    textInPutFileDir.setText(files.getAbsolutePath());
    else
    JOptionPane.showMessageDialog(null, "Please Select a Directory for Input file");
    }

    }
    });
    button.setBounds(624, 135, 66, 20);
    panel_1.add(button);

    The i am saving this path in config.properties file with below code.

    private void saveProperties() throws IOException {
    configProps.setProperty("InPutFileDir", textInPutFileDir.getText());
    OutputStream outputStream = new FileOutputStream(configFile);
    configProps.store(outputStream, "ConfigFile");
    outputStream.close();
    }

    Once the path is saved in to config.properties file. I opened the file and check the path. Here the path is coming in wrong format. with above code path is saving in 'E\:\\Downloads\\Java' format. But i need this path to be saved in 'E:\\Downloads\\Java' format.

    And extra \ is coming between the Drive name and colon.
    Can someone please help me to achieve this. Please provide the example if possible. That would really help in understanding the issue.

    Thank you.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Directory Path written in wrong format in config.properties file

    What problems are you having with that properties file? Does some program give you an error?

    The \ is used to escape certain special characters. The : is a special character that needs to be escaped so the \ is added when the file is written and removed with the file is read.

    A couple of lines from a properties file on my Windows7 system:
    lastRSSFile=E\:\\DigitalImages\\2015_Europe.NSS
    hndrdPercent=false
    jpgFolder=E\:\\DigitalImages\\2015_Europe\\139___0 5
    Last edited by Norm; 07-24-2015 at 01:27 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Directory Path written in wrong format in config.properties file

    This is normal. You can see this behaviour in the source of the Properties class:
    Java Code:
    for(int x=0; x<len; x++) {
        char aChar = theString.charAt(x);
        // Handle common case first, selecting largest block that
        // avoids the specials below
        if ((aChar > 61) && (aChar < 127)) {
            if (aChar == '\\') {
                outBuffer.append('\\'); outBuffer.append('\\');
                continue;
            }
            outBuffer.append(aChar);
            continue;
        }
        switch(aChar) {
            case ' ':
                if (x == 0 || escapeSpace)
                    outBuffer.append('\\');
                outBuffer.append(' ');
                break;
            case '\t':outBuffer.append('\\'); outBuffer.append('t');
                break;
            case '\n':outBuffer.append('\\'); outBuffer.append('n');
                break;
            case '\r':outBuffer.append('\\'); outBuffer.append('r');
                break;
            case '\f':outBuffer.append('\\'); outBuffer.append('f');
                break;
            case '=': // Fall through
            case ':': // Fall through
            case '#': // Fall through
            case '!':
                outBuffer.append('\\'); outBuffer.append(aChar);
                break;
            default:
                if (((aChar < 0x0020) || (aChar > 0x007e)) & escapeUnicode ) {
                    outBuffer.append('\\');
                    outBuffer.append('u');
                    outBuffer.append(toHex((aChar >> 12) & 0xF));
                    outBuffer.append(toHex((aChar >>  8) & 0xF));
                    outBuffer.append(toHex((aChar >>  4) & 0xF));
                    outBuffer.append(toHex( aChar        & 0xF));
                } else {
                    outBuffer.append(aChar);
                }
        }
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Directory Path written in wrong format in config.properties file

    Basically the colon is being escaped, since it has a special meaning in properties files.

    java - Properties file backslash and semicolon - Stack Overflow


    ... which Norm already mentioned, but I'm blind.
    Last edited by gimbal2; 07-24-2015 at 10:58 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    dmk.3678 is offline Member
    Join Date
    Jun 2015
    Posts
    8
    Rep Power
    0

    Default Re: Directory Path written in wrong format in config.properties file

    Hi Norm,

    Yes i am getitng the same format like you do, as you show here lastRSSFile=E\:\\DigitalImages\\2015_Europe.NSS the extra \ is getting added while saving the Directory path in properties file. So my another program is not able to read this path properly. Can you suggest something to over come this problem with example please ?

  6. #6
    dmk.3678 is offline Member
    Join Date
    Jun 2015
    Posts
    8
    Rep Power
    0

    Default Re: Directory Path written in wrong format in config.properties file

    Quote Originally Posted by Norm View Post
    What problems are you having with that properties file? Does some program give you an error?

    The \ is used to escape certain special characters. The : is a special character that needs to be escaped so the \ is added when the file is written and removed with the file is read.

    A couple of lines from a properties file on my Windows7 system:
    lastRSSFile=E\:\\DigitalImages\\2015_Europe.NSS
    hndrdPercent=false
    jpgFolder=E\:\\DigitalImages\\2015_Europe\\139___0 5


    Hi Norm,

    Yes i am getting the same format like you do, as you show here lastRSSFile=E\:\\DigitalImages\\2015_Europe.NSS the extra \ is getting added while saving the Directory path in properties file. So my another program is not able to read this path properly. Can you suggest something to over come this problem with example please ?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Directory Path written in wrong format in config.properties file

    Use the same set of classes that were used to write the file to read it. The data is in a Properties file format so use the Properties class's method to read it.
    Or write the String to a text file (instead of using Properties class methods) and then the file can be read with a normal file reading class without worrying about the escape character.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    dmk.3678 is offline Member
    Join Date
    Jun 2015
    Posts
    8
    Rep Power
    0

    Default Re: Directory Path written in wrong format in config.properties file

    Quote Originally Posted by Norm View Post
    Use the same set of classes that were used to write the file to read it. The data is in a Properties file format so use the Properties class's method to read it.
    Or write the String to a text file (instead of using Properties class methods) and then the file can be read with a normal file reading class without worrying about the escape character.
    Thanks Norm. I finally got it working with your suggestion.
    Also would like to know if we can suppress or make return "Success" string not come up as popup in below code.

    public String execute() throws Exception{
    for( String fileName : files.split(","))
    {
    //Do blah blah
    }
    return "Success";
    }

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Directory Path written in wrong format in config.properties file

    make return "Success" string not come up as popup
    Please explain. I don't see any popup in that code. The code shows a method that returns a String.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    dmk.3678 is offline Member
    Join Date
    Jun 2015
    Posts
    8
    Rep Power
    0

    Default Re: Directory Path written in wrong format in config.properties file

    Quote Originally Posted by Norm View Post
    Please explain. I don't see any popup in that code. The code shows a method that returns a String.
    I am working on a swing app where i select one file and then app converts it to text format with some additional delimiters.
    after successful, it gives a popup saying "Success". See attached screenshot.

    Following is my code. I have bold the section which return this popup.

    public class ReadWriteController {

    public static String writeTxt(String fileName1 ,String outPutDirectory,String columnDelimeter,String startDelimeter,String endDemiliter,Updater updater)
    {
    java.util.Date logDate = new java.util.Date();
    String df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS").format(logDate);
    String currentFile="";
    String files=fileName1;

    CurrentJob.numberOfFiles=files.split(",").length;
    for( String fileName : files.split(","))
    {
    currentFile=fileName;
    File file=new File(fileName);
    if(file.exists())
    {

    Date date = new Date();
    Format formatter = new SimpleDateFormat("MMM_dd_HH_mm_yyyy");
    String NewDate = formatter.format(date);
    String outputFileName=outPutDirectory+System.getProperty( "file.separator")+NewGuiApp.textField_5.getText()+ "_"+file.getName()+"_"+NewDate.toString().replace( " ", "_").replace(":", "_")+NewGuiApp.textField_format.getText();
    //System.out.println(outputFileName);
    TxtFileWriter fileWriter = new TxtFileWriter(outputFileName);
    try {
    //System.out.println(FileTypeUtiliy.isText(file, true));
    if( !FileTypeUtiliy.isText(file, true))
    {
    System.out.println("Excel");


    ReadExcelFile excelFile1 =new ReadExcelFile();
    excelFile1.readFile(fileName, fileWriter, columnDelimeter, startDelimeter, endDemiliter,updater);

    }
    else if (FileTypeUtiliy.isHTML(fileName))
    {
    System.out.println("Html");

    ReadHTMLFile htmlFile = new ReadHTMLFile();
    htmlFile.readFile(fileName, fileWriter, columnDelimeter, startDelimeter, endDemiliter,updater);
    }
    else
    {
    System.out.println("csv");

    ReadCSVFile csvFile = new ReadCSVFile();
    csvFile.readFile(fileName, fileWriter, columnDelimeter, startDelimeter, endDemiliter,updater);
    }
    fileWriter.close();
    if(CurrentJob.deleteFile)
    {

    file.delete();

    }
    } catch (Exception e) {

    CwLog.LOGGER.error(df+": "+currentFile+" Unable to process this file stoping furthur processing ");
    }

    CwLog.LOGGER.info(df+": "+"Successfully Converted File "+currentFile+" to "+NewGuiApp.textField_format.getText()+" Format.");
    CwLog.LOGGER.info(df+": "+"Converted File Is uploaded to :- "+outputFileName);
    }

    }
    return "Success";
    }
    public static void main(String[] args) {
    System.out.println( ReadWriteController.writeTxt("D:\\DataAdapter.jar" , "D:\\", "|", "", "",new Updater(new JProgressBar())));
    }
    }
    Attached Thumbnails Attached Thumbnails Directory Path written in wrong format in config.properties file-000232.jpg  

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Directory Path written in wrong format in config.properties file

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. How to give class path for .properties file
    By kewlkeny in forum Spring
    Replies: 1
    Last Post: 01-23-2012, 02:40 PM
  2. how can i get directory path without select any file ?
    By amr magdy in forum New To Java
    Replies: 1
    Last Post: 12-29-2011, 11:52 AM
  3. Lo4j issue - logs are written in a wrong file
    By Dhamo in forum Advanced Java
    Replies: 0
    Last Post: 05-09-2010, 09:11 PM
  4. Path for properties file and license
    By aleks.farrier in forum Advanced Java
    Replies: 0
    Last Post: 08-03-2009, 03:12 PM
  5. Weird path problem when reading properties file
    By jerry_popperq in forum New To Java
    Replies: 0
    Last Post: 03-18-2009, 03:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •