Richard
Richard

Reputation: 35

How to open a file that contains Chinese characters, in java IO?

import java.io.*;
public class JavaIO {
    public static void main(String[] args) {
        FileInputStream fis=null;
        try{
            fis = new FileInputStream("F:\Java的提高学习\from.txt");
        }  
        catch(Exception e ){
            System.out.println(e);
        }
    }
}

fis = new FileInputStream("F:\Java的提高学习\from.txt") causes an error due to the Chinese characters in the path name of the file. Please help me deal with this problem.

Upvotes: 2

Views: 511

Answers (1)

It's not because of the Chinese characters.

In a Java string, \n represents a newline. \t represents a tab. \" represents a quote mark. \\ represents a single \. There are a few more that are less commonly used. These sets of "\ then another character" are called escape sequences.

\J is an invalid escape sequence. \f is a valid escape sequence but it's not what you want.

To put an actual backslash in a string, you need to use the \\ escape sequence instead. Like this:

fis = new FileInputStream("F:\\Java的提高学习\\from.txt");

Upvotes: 5

Related Questions