List of usage examples for java.io PrintWriter close
public void close()
From source file:Main.java
public static void main(String args[]) throws Exception { PrintWriter pw = new PrintWriter(new FileWriter("logfile.txt", true)); timeStamp("File opened", pw); pw.close(); if (pw.checkError()) System.out.println("I/O error occurred."); }
From source file:Main.java
public static void main(String[] args) { try {//from w ww.jav a 2s. c o m char[] chars = new char[2]; chars[0] = '\u4F60'; chars[1] = '\u597D'; String encoding = "GB18030"; File textFile = new File("C:\\temp\\myFile.txt"); PrintWriter writer = new PrintWriter(textFile, encoding); writer.write(chars); writer.close(); // read back InputStreamReader reader = new InputStreamReader(new FileInputStream(textFile), encoding); char[] chars2 = new char[2]; reader.read(chars2); System.out.print(chars2[0]); System.out.print(chars2[1]); reader.close(); } catch (IOException e) { System.out.println(e.toString()); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { FileWriter file = new FileWriter("a.html"); BufferedWriter buffer = new BufferedWriter(file); PrintWriter out = new PrintWriter(buffer); out.println("<html>\n\t<body>"); out.println("\t</body>\n</html>"); out.close(); buffer.close();//from w w w . ja va2s . c o m file.close(); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { KeyGenerator kg = KeyGenerator.getInstance("DES"); kg.init(new SecureRandom()); SecretKey key = kg.generateKey(); SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); Class spec = Class.forName("javax.crypto.spec.DESKeySpec"); DESKeySpec ks = (DESKeySpec) skf.getKeySpec(key, spec); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("keyfile")); oos.writeObject(ks.getKey());/*w w w.j a v a 2s.co m*/ Cipher c = Cipher.getInstance("DES/CFB8/NoPadding"); c.init(Cipher.ENCRYPT_MODE, key); CipherOutputStream cos = new CipherOutputStream(new FileOutputStream("ciphertext"), c); PrintWriter pw = new PrintWriter(new OutputStreamWriter(cos)); pw.println("Stand and unfold yourself"); pw.close(); oos.writeObject(c.getIV()); oos.close(); }
From source file:base64test.Base64Test.java
/** * @param args the command line arguments *//* w w w . j a v a 2 s . c o m*/ public static void main(String[] args) { try { if (!Base64.isBase64(args[0])) { throw new Exception("Arg 1 is not a Base64 string!"); } else { String decodedBase64String = new String(Base64.decodeBase64(args[0])); File tempFile = File.createTempFile("base64Test", ".tmp"); tempFile.deleteOnExit(); FileWriter fw = new FileWriter(tempFile, false); PrintWriter pw = new PrintWriter(fw); pw.write(decodedBase64String); pw.close(); String fileType = getFileType(tempFile.toPath()); System.out.println(fileType); System.in.read(); } } catch (Exception e) { System.err.println(e.getMessage()); } }
From source file:MainClass.java
public static void main(String[] args) { try {//from w w w .j a v a2 s .c om char[] chars = new char[2]; chars[0] = '\u4F60'; chars[1] = '\u597D'; String encoding = "GB18030"; File textFile = new File("C:\\temp\\myFile.txt"); PrintWriter writer = new PrintWriter(textFile, encoding); writer.write(chars); writer.close(); // read back InputStreamReader reader = new InputStreamReader(new FileInputStream(textFile), encoding); char[] chars2 = new char[2]; reader.read(chars2); System.out.print(chars2[0]); System.out.print(chars2[1]); reader.close(); } catch (IOException e) { System.out.println(e.toString()); } }
From source file:MainClass.java
public static void main(String[] a) throws Exception { URL url = new URL("http://www.yourdomain.com/form.jsp"); URLConnection connection = url.openConnection(); connection.setDoOutput(true);//from www.j av a 2 s. c om PrintWriter out = new PrintWriter(connection.getOutputStream()); out.println("firstName=Joe"); out.println("lastName=Average"); out.close(); }
From source file:MainClass.java
public static void main(String[] args) { try {/* w ww .j ava2 s.c o m*/ PrintWriter pw = new PrintWriter("c:\\temp\\printWriterOutput.txt"); pw.println("PrintWriter is easy to use."); pw.println(1234); pw.close(); } catch (IOException e) { } }
From source file:WriteFile.java
public static void main(String[] args) { Product[] movies = new Product[10]; movies[0] = new Product("L", 1946, 14.95); movies[1] = new Product("T", 1965, 12.95); movies[2] = new Product("Y", 1974, 16.95); movies[3] = new Product("T", 1975, 11.95); movies[4] = new Product("S", 1977, 17.95); movies[5] = new Product("B", 1987, 16.95); movies[6] = new Product("G", 1989, 14.95); movies[7] = new Product("A", 1995, 19.95); movies[8] = new Product("G", 1997, 14.95); movies[9] = new Product("R", 2001, 19.95); PrintWriter out = openWriter("movies.txt"); for (Product m : movies) writeMovie(m, out);//ww w. ja v a 2s .co m out.close(); }
From source file:LogTest.java
public static void main(String[] args) throws IOException { String inputfile = args[0];/*from w w w .j ava 2s. c o m*/ String outputfile = args[1]; Map<String, Integer> map = new TreeMap<String, Integer>(); Scanner scanner = new Scanner(new File(inputfile)); while (scanner.hasNext()) { String word = scanner.next(); Integer count = map.get(word); count = (count == null ? 1 : count + 1); map.put(word, count); } scanner.close(); List<String> keys = new ArrayList<String>(map.keySet()); Collections.sort(keys); PrintWriter out = new PrintWriter(new FileWriter(outputfile)); for (String key : keys) out.println(key + " : " + map.get(key)); out.close(); }