llegal escape character followed by a space

流过昼夜 提交于 2019-12-04 05:34:36

问题


I'm writing a bit of code to run a shell script using process that loads and runs a file in terminal. The problem I'm having is getting the filename to be recognised by terminal due to the spaces, for example :

"$ ./run_file.sh foo bar.ss" 

should be run in terminal as

"$ ./run_file.sh foo\ bar.ss"

Heres the code to change it replace it:

JPanel panel1 = new JPanel();
JButton button = new JButton("Run");
button.setAlignmentX( Component.CENTER_ALIGNMENT);

button.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent event){

        run();

    }

});
//button.setAlignmentX(0.5);
panel1.add(button);
panel1.add(Box.createVerticalGlue());
panel1.add(button);

menuB = new JMenuBar();

JMenu dropD = new JMenu("File");
menuB.add(dropD);

JMenuItem loadR = new JMenuItem("Load file");
JMenuItem quit = new JMenuItem("Quit");
dropD.add(loadR);
dropD.add(quit);
loadR.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event) {
            JFileChooser fileopen = new JFileChooser();

            int r= fileopen.showDialog(panel, "Load file");

            if (r == JFileChooser.APPROVE_OPTION) {
                File file = fileopen.getSelectedFile();
                String string = file.toString();
                string = string.replaceAll(" ", "\ ");
                //String output = aa.replaceAll("/",Character.toString(File.separatorChar));
                System.out.println(string);
                loadFile = file;
        }

       }
  });

I have been tried using String.replaceAll but get

java:66: illegal escape character

i realise that I can use File.separatorChar :

string = string.replaceAll(" ", Character.toString(File.separatorChar)+" ");

but this doesn't seem to replace anything... Any help would be much appreciated.

Thanks


回答1:


If you want to put the \ character (which is the escape character) inside a string, you'll need to escape it:

string = string.replaceAll (" ", "\\ ");

A single \ is a escape sequence leading character, such as with \n (newline) or \r (carriage return). The full list of single-character escapes is:

\b    backspace
\t    tab
\n    linefeed (newline)
\f    form feed
\r    carriage return
\"    double quote
\'    single quote
\\    backslash

This is in addition to the octal escape sequences s such as \0, \12 or \377.

The reason why your separatorChar solution won't work is because that gives you the separator char (/ under UNIX and its brethren), not the escape character \ that you need.




回答2:


If you want the string to contain an actual backslash you need to escape the backslash. Otherwise javac thinks you're trying to escape space, which doesn't need escaping:

string = string.replaceAll(" ", "\\ ");

Using this code, the second argument to the method will be a 2-character string: backslash followed by space. I assume that's what you want.

See section 3.10.6 of the Java Language Specification for more details of character/string literal escape sequences.



来源:https://stackoverflow.com/questions/9532596/llegal-escape-character-followed-by-a-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!