public void testWriteUTF8() throws Exception { String toWrite = "Hello I'm a UTF-8 string that is using Umlauts - alt - älter - am ältesten"; // The default FileWriter encoding is already a UTF-8, so no special writers decoration is // needed. WriterOutputStream os = new WriterOutputStream(new FileWriter(testFile), "UTF-8"); os.write(toWrite.getBytes()); os.close(); // Read back and test that the encoding is OK BufferedReader reader = new BufferedReader(new FileReader(testFile)); String line = reader.readLine(); reader.close(); assertEquals("UFT-8 write-read process failed", toWrite, line); }
public void testWriteBytesWithCharset() throws Exception { String shortS = "P"; String longS = "A B C"; WriterOutputStream os = new WriterOutputStream(new FileWriter(testFile), "UTF-8"); byte[] bytes = longS.getBytes(); os.write(shortS.getBytes()[0]); os.write(bytes, 0, 3); os.close(); // Verify the written data BufferedReader reader = new BufferedReader(new FileReader(testFile)); String line = reader.readLine(); reader.close(); assertEquals("Write-read process failed", "PA B", line); }
public void testWriteCP1255() throws Exception { String toWrite = "Hello I'm a CP-1255 string that has Hebrew - שלום"; WriterOutputStream os = new WriterOutputStream( new OutputStreamWriter(new FileOutputStream(testFile), "CP1255"), "CP1255"); os.write(toWrite.getBytes("CP1255")); os.flush(); os.close(); // Read back and test that the encoding is OK BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(testFile), "CP1255")); String line = reader.readLine(); reader.close(); assertEquals("CP1255 write-read process failed", toWrite, line); }