Note that there are some explanatory texts on larger screens.

plurals
  1. POJava fast stream copy with ISO-8859-1
    primarykey
    data
    text
    <p>I have the following code, which will read in files in ISO-8859-1, as thats what is required in this application, </p> <pre><code>private static String readFile(String filename) throws IOException { String lineSep = System.getProperty("line.separator"); File f = new File(filename); StringBuffer sb = new StringBuffer(); if (f.exists()) { BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(filename), "ISO-8859-1")); String nextLine = ""; while ((nextLine = br.readLine()) != null) { sb.append(nextLine+ " "); // note: BufferedReader strips the EOL character. // sb.append(lineSep); } br.close(); } return sb.toString(); } </code></pre> <p>The problem is it is pretty slow. I have this function, which is MUCH faster, but I can not seem to find how to place the character encoding:</p> <pre><code>private static String fastStreamCopy(String filename) { String s = ""; FileChannel fc = null; try { fc = new FileInputStream(filename).getChannel(); MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); int size = byteBuffer.capacity(); if (size &gt; 0) { byteBuffer.clear(); byte[] bytes = new byte[size]; byteBuffer.get(bytes, 0, bytes.length); s = new String(bytes); } fc.close(); } catch (FileNotFoundException fnfx) { System.out.println("File not found: " + fnfx); } catch (IOException iox) { System.out.println("I/O problems: " + iox); } finally { if (fc != null) { try { fc.close(); } catch (IOException ignore) { } } } return s; } </code></pre> <p>Any one have an idea of where i should be putting the ISO encoding?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload