printwriter

JavaIO学习:打印流

两盒软妹~` 提交于 2019-11-26 17:10:00
打印流 打印流是输出信息最方便的类,注意包含字节 打印流:PrintStream和字符打印流:PrintWriter。 打印流提供了非常方便的打印功能,可以打印任何类型的数据信息,例如 :小数,整数,字符串。 回顾: 之前打印信息需要使用OutputStream但是这样,所有数据输出会非常麻烦,String-->byte[],打印流中可以方便进行输出, 打印流好处 通过定义的构造方法可以发现,有一个构造方法可以 直接接收OutputStream类的实例, 与OutputStream相比起来,PrintStream可以更方便的输出数据,相当于把OutputStream类重新包装了一下,使之输出更方便。 格式化输出 JAVA对PrintStream功能进行了扩充,增加了格式化输出功能。直接使用Print即可。但是输出的时候需要指定输出的数据类型。 打印流: PrintStream 和PrintWriter 实现将基本数据类型的数据格式转化为字符串输出。 说明: 提供了一系列重载的print()和println()方法,用于多种数据类型的输出。 PrintStream和PrintWriter的输出不会抛出IOException异常。 PrintStream和PrintWriter有自动flush功能。 PrintStream 打印的所有字符都使用平台的默认字符编码转换为字节。

PrintWriter append method not appending

穿精又带淫゛_ 提交于 2019-11-26 14:41:00
The following method only writes out the latest item I have added, it does not append to previous entries. What am I doing wrong? public void addNew() { try { PrintWriter pw = new PrintWriter(new File("persons.txt")); int id = Integer.parseInt(jTextField.getText()); String name = jTextField1.getText(); String surname = jTextField2.getText(); Person p = new Person(id,name,surname); pw.append(p.toString()); pw.append("sdf"); pw.close(); } catch (FileNotFoundException e) {...} } The fact that PrintWriter 's method is called append() doesn't mean that it changes mode of the file being opened. You

Java基础之Socket,TCP/IP篇

落花浮王杯 提交于 2019-11-26 12:12:29
网络编程,两种方法;使用TCP/IP(性能低,数据较安全) 或 UDP(性能高,数据不安全) TCP/IP 方式,服务器端(注意使用PrintWriter + OutputStream方法才能刷出数据) 1 package com.imooc.demo.tcpip; 2 3 import java.io.*; 4 import java.net.ServerSocket; 5 import java.net.Socket; 6 import java.util.Scanner; 7 8 /** 9 * TCP/IP协议的网络编程 10 * 双向连接 11 * 服务器端 12 */ 13 public class ServerDemo { 14 15 public static void main(String[] args) throws Exception{ 16 Scanner scanner = new Scanner(System.in); 17 System.out.println("服务端准备就绪!"); 18 ServerSocket server = new ServerSocket(8888); 19 Socket client = server.accept(); 20 System.out.println(client+"进入服务器"); 21

PrintWriter vs FileWriter in Java

心不动则不痛 提交于 2019-11-26 11:58:41
问题 Are PrintWriter and FileWriter in Java the same and no matter which one to use? So far I have used both because their results are the same. Is there some special cases where it makes sense to prefer one over the other? public static void main(String[] args) { File fpw = new File(\"printwriter.txt\"); File fwp = new File(\"filewriter.txt\"); try { PrintWriter pw = new PrintWriter(fpw); FileWriter fw = new FileWriter(fwp); pw.write(\"printwriter text\\r\\n\"); fw.write(\"filewriter text\\r\\n\"

Java: Difference between PrintStream and PrintWriter

亡梦爱人 提交于 2019-11-26 08:45:58
问题 What is the difference between PrintStream and PrintWriter ? They have many methods in common due to which I often mix these two classes up. Moreover, I think we can use them for exactly the same things. But there has to be a difference, otherwise, there would have been only one class. I have searched the archives, but couldn\'t find this question. 回答1: This might sound flippant, but PrintStream prints to an OutputStream , and PrintWriter prints to a Writer . Ok, I doubt I'll get any points

Java - delete line from text file by overwriting while reading it

安稳与你 提交于 2019-11-26 07:49:06
问题 I\'m trying to delete a line of text from a text file without copying to a temporary file. I am trying to do this by using a Printwriter and a Scanner and having them traverse the file at the same time, the writer writing what the Scanner reads and overwriting each line with the same thing, until it gets to the line that I wish to delete. Then, I advance the Scanner but not the writer, and continue as before. Here is the code: But first, the parameters: My file names are numbers, so this

PrintWriter append method not appending

心已入冬 提交于 2019-11-26 03:44:55
问题 The following method only writes out the latest item I have added, it does not append to previous entries. What am I doing wrong? public void addNew() { try { PrintWriter pw = new PrintWriter(new File(\"persons.txt\")); int id = Integer.parseInt(jTextField.getText()); String name = jTextField1.getText(); String surname = jTextField2.getText(); Person p = new Person(id,name,surname); pw.append(p.toString()); pw.append(\"sdf\"); pw.close(); } catch (FileNotFoundException e) {...} } 回答1: The

How to use PrintWriter and File classes in Java?

耗尽温柔 提交于 2019-11-26 02:34:55
问题 I am trying to understand PrintWriter for a small program I\'m making, and I cant seem to get java to make the file and then write on it. When I execute the program below it gives me a Filenotfoundexeption error on line 9. It also fails to make the file in the directory that I specified. I am new to this so please try and keep the answers simple. I am using Eclipse. import java.io.PrintWriter; import java.io.File; public class Testing { public static void main(String[] args) { File file = new