printwriter

  • Post author:
  • Post category:其他


import java.io.FileNotFoundException;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.List;import java.util.Set;import java.util.concurrent.atomic.AtomicInteger;public class PrintWriterTest { public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException { PrintWriter writer = new PrintWriter(“the-file-name.txt”, “UTF-8”); writer.println(“The first line”); writer.println(“The second line”); writer.close(); } public static void printFile(String fileName, HashMap<String, List> map) { int size = 4000; Set set = map.keySet(); set.forEach(key -> { List list = map.get(key); int max = 1; int page = 1; boolean flag = true; do { max = page * size; if (max >= list.size()) { max = list.size(); flag = false; } try { PrintWriter writer = new PrintWriter(fileName + “-” + key + “-” + max, “UTF-8”); for (int i = (page – 1) * size; i < max; i++) { writer.println(list.get(i) + “,”); } writer.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } page++; } while (flag); }); }}



版权声明:本文为baiducheng原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。