Класс PrintStream служит для расширения классов потоков вывода, чтобы работать с ними через методы print(), println(), printf(). Можно передать конструктору потока PrintStream файловый поток вывода и простыми методами выводить построчно любые данные. Пример:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javainputoutput;
/** * * @author martyshov */ import java.io.*; import java.nio.*; import java.util.*;
public class JavaInputOutput {
/** * @param args the command line arguments */ public static void main(String[] args) throws IOException {
PrintStream ps = null; try {
ps = new PrintStream(new FileOutputStream(new File("C:/test.txt"))); ps.println("Запишем строку в файл"); ps.println(new Integer(100)); ps.println(777); ps.println(); ps.println('$'); char[] ch_arr = {'A', 'r', 'r', 'a', 'y'}; ps.println(ch_arr); class MyClass{ int field1 = 100; double field2 = 600.06;
@Override public String toString() { return "тип класса MyClass{" + "field1=" + field1 + ", field2=" + field2 + '}'; } } ps.println(new MyClass());
} catch (IOException e) { System.err.println(e.getLocalizedMessage()); } finally { if (ps != null) ps.close(); }
} }
|