mercredi 28 octobre 2015

My (String).split("="); isnt working?

I wanted to make a string to a String[] but it isnt working how i wanted it to work! My code:

    public static void get(HashMap<String, String> saves, File file) throws UnsupportedEncodingException, FileNotFoundException, IOException{
    if (!file.exists()){
        return;
    }
    InputStreamReader reader;
    reader = new InputStreamReader(new FileInputStream(file), "UTF-16");
    String r = null;
    String[] s;
    BufferedReader bufreader = new BufferedReader(reader);
    while((r=bufreader.readLine()) != null){
        s = r.split("=");
        if (s.length < 2){
            System.out.println(s.length);
            System.out.println(s[0]);
            return;
        }
        saves.put(s[0].toString(), s[1].toString());
        s = null;
    }
}

And also when i tell it to println the String to the console

System.out.println(s.length);
System.out.println(s[0]);

it just prints:

1
??????????????????
 - 
 - 

What it should be reading (What is in the file):

1=welcome
2=hello
3=bye
4=goodbye

So i want it to put the values in to the hashmap:

saves.put("1", "welcome");
saves.put("2", "hello");
saves.put("3", "bye");
saves.put("4", "goodbye");

but the s = e.split("=") is not splitting it is making the String to "?????????" Thank you!

Aucun commentaire:

Enregistrer un commentaire