0

エンコーディング/文字セットを失うことなく、OutputStream(url.openStream())からWriter(FileWriterまたはStringwriterの場合もあります)に書き込む正しい方法は何ですか?

public static String parseURLContentIntoFile(final URL url, final File fileToFill) throws IOException{      
    return parseURLContentIntoOutputWriter(url, new OutputStreamWriter(FileUtils.openOutputStream(fileToFill),"UTF-8"));        
}

public static String parseURLContentIntoString(final URL url) throws IOException{
    final StringWriter output = new StringWriter(); // Or StringBuilderWriter
    parseURLContentIntoOutputWriter(url, output);
    return output.getBuffer().toString();   //Or output.getBuilder().toString()
}


private static String parseURLContentIntoOutputWriter(final URL url, final Writer writer) throws IOException{

    InputStreamReader in = null;
    BufferedWriter out = null;
    try {
        out = new BufferedWriter(writer);
        in = new InputStreamReader(url.openStream(),"UTF-8");
        for(String line : IOUtils.readLines(in)){ //Uses a buffer internally
                      (...VERY LONG parsing...)
          if (!line.isEmpty()) IOUtils.write(line,out); 
        }
        (...Exception handling and stream closing...)
 }

ありがとう!

4

0 に答える 0