printwriter

FileWrite BufferedWriter and PrintWriter combined

老子叫甜甜 提交于 2019-11-27 19:28:39
Ok so I am learning about I/O, and I found the following code in one of the slides. can someone please explain why there is a need to have a FileWrite, BufferedWriter and PrintWriter? I know BufferedWriter is to buffer the output and put it all at once but why would they use FileWriter and PrintWriter ? dont they pretty much do the same with a bit of difference in error handling etc? And also why do they pass bw to PrintWriter ? FileWriter fw = new FileWriter (file); BufferedWriter bw = new BufferedWriter (fw); PrintWriter outFile = new PrintWriter (bw); Presumably they're using a FileWriter

socket 实现点对点发送消息

流过昼夜 提交于 2019-11-27 19:10:36
socket:套接字,描述 ip 和 port;socket 在建立网络连接时使用;通过socket可以向网络发送请求以及应答网络请求 serverSocket:主要应用于服务器端,监听 socket 连接 具体示例代码如下: package socket; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; /** * 客户端 * @author hualei * @date Sep 3, 2017 4:26:34 PM * */ public class Client { private Socket socket; private String alias; public Client (){ try { socket = new Socket( "127.0.0.1" , 8088 ); // socket

网络编程

强颜欢笑 提交于 2019-11-27 16:42:01
计算机网络 网络中数据交流 IP: Ipv4: 32 位,唯一标识计算机 Ipv6: 128 位 端口号: 唯一标识进程(软件)找到是程序 package cn.jiedada.getip; import java.net.InetAddress; import java.net.UnknownHostException; public class InetTest { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub InetAddress localHost = InetAddress.getLocalHost(); System.out.println(localHost);//DESKTOP-0H89I0E/172.16.9.175 InetAddress[] allByName = InetAddress.getAllByName("DESKTOP-0H89I0E"); for (InetAddress inetAddress : allByName) { System.out.println(inetAddress); } InetAddress name = InetAddress.getByName("baidu.com");

Java PrintWriter not working

混江龙づ霸主 提交于 2019-11-27 16:25:36
I am simply trying to write my 2d array "puzzle" to a file. I have a double for loop which reads through each of the 'char' values in my array and supposedly writes them to the file. I can't seem to find the error in my code. The file says it is modified when I run the program, but it is still blank. Thanks guys! public void writeToFile(String fileName) { try{ PrintWriter pW = new PrintWriter(new File(fileName)); for(int x = 0; x < 25; x++) { for(int y = 0; y < 25; y++) { pW.write(puzzle[x][y]); } pW.println(); } } catch(IOException e) { System.err.println("error is: "+e.getMessage()); } }

JavaIO流:(五)打印流

一世执手 提交于 2019-11-27 16:01:49
PrintStream PrintStream 提供了一系列的 print()和 println(),可以实现将基本数据类型格式化成字符串输出。对象类型将先调用toString(),然后输出该方法返回的字符串 System.out 就是 PrintStream 的一个实例,代表显示器 System.err 也是 PrintStream 的一个实例,代表显示器 PrintStream的输出功能非常强大,通常需要输出文本内容,都可以将输出流包装成 PrintStream 后进行输出 PrintStream 的方法都不抛出 IOException PrintWriter PrintStream 的对应字符流,功能相同,方法对应。 PrintWriter 的方法也不抛出 IOException 复制文件时可以使用PrintWriter代替BufferedWriter完成,更简单 来源: https://blog.csdn.net/qq_45017999/article/details/99694821

打印流

梦想的初衷 提交于 2019-11-27 15:46:56
打印流可以分为:字节打印流PrintStream和字符打印流PrintWriter,都不会抛出IO异常。能够很方便地打印各种数据类型的数据。字符流可以实现自动刷新。 1、打印流PrintStream输出数据: (1)不具有自动刷新功能 import java.io.FileNotFoundException; import java.io.PrintWriter; public class PrintWriterDemo { public static void main(String[] args) throws FileNotFoundException { PrintWriter pw = new PrintWriter("E:\\IO流\\java\\221.txt"); pw.println("打印流"); pw.print(0.123); pw.close(); } } (2)创建自动刷新的打印流 a、与 PrintStream 类不同,如果启用了自动刷新,则只有在调用 println 、 printf 或 format 的其中一个方法时才可能完成此操作 b、数据目的必须是流对象 OutputStream、Writer,因为构造方法中具有自动刷新功能的只有如下两个: import java.io.FileNotFoundException; import java.io

Java 操作Word书签(一):添加、删除、读取书签

笑着哭i 提交于 2019-11-27 10:14:07
Word中,书签功能常用于查找、定位、标记特定字符或段落,对于篇幅较大的文档,此功能非常实用。下面,将介绍通过Java程序来添加及删除Word书签的方法。示例要点包括: 1. 添加书签 1.1 给指定段落添加书签 1.2 给指定字符串添加书签 2. 删除书签 2.1删除书签 2.2 删除书签文本 3. 读取书签文本 使用工具: Free Spire.Doc for Java (免费版) Jar 文件获取及导入: 方法 1 : 通过官网 下载jar 文件包。下载后,解压文件。并将lib文件夹下的Spire.Doc.jar文件导入到java程序。参考如下导入效果: 方法 2 : 可通过maven仓库 安装导入 。可参考安装 导入方法 。 Java 代码示例 【示例 1 】给指定段落添加书签 import com.spire.doc.*; import com.spire.doc.documents.Paragraph; public class AppendBookmark { public static void main(String[]args){ //加载需要添加书签的Word文档 Document doc = new Document(); doc.loadFromFile("sample.docx"); //获取需要添加书签的段落 Paragraph para = doc

PrintWriter vs FileWriter in Java

梦想与她 提交于 2019-11-27 06:23:34
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"); pw.close(); fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

What is the exact difference between out.write() and out.print()

时光毁灭记忆、已成空白 提交于 2019-11-26 22:51:07
问题 In my servlet I gave both out.print and out.write . but both prints in the browser. What is the exact difference between these two and when to use out.print and out.write ? 回答1: The basic difference is that out.write() explodes if you pass it a null: String s = null; out.print(s); // outputs the text "null" out.write(s); // NullPointerException 回答2: PrintWriter: public void write(String s) Write a string. This method cannot be inherited from the Writer class because it must suppress I/O

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

倾然丶 夕夏残阳落幕 提交于 2019-11-26 21:03:10
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 would read 1.txt or 2.txt, etc, and so f specifies the file name. I convert it to a String in the