printwriter

closing nested streams [duplicate]

房东的猫 提交于 2019-11-29 14:29:42
Possible Duplicate: Best way to close nested streams in Java? How we close nested streams? Closing all of them? If yes what is the order? FileOutputStream out = new FileOutputStream("data.txt", true); PrintWriter pout = new PrintWriter(out); /* do some I/O */ pout.close(); out.close(); or closing the the out most stream will close all of them. pout.close(); // Is this enough? MD Sayem Ahmed When closing chained streams, you only need to close the outermost stream. Any errors will be propagated up the chain and be caught. Take a look at here . Probably this question has been asked before.

Print类详解

冷暖自知 提交于 2019-11-29 13:18:04
PrintStream:字节打印流说明 提供了打印方法可以对多种数据类型值进行打印。并保持数据的表示形式。 注意:这里说的打印方法是print, 此方法可以直接打印数据的原有形式不做编码转换,也就是下面介绍的print方法. 也就是说想要保存数据原样性,推荐使用此类。 它不抛IOException. 构造函数接收三种类型的值: 字符串路径。 File对象。 字节输出流。 注意    PrintStream 打印的所有字符都使用平台的默认字符编码转换为字节。在需要写入字符而不是写入字节的情况下,应该使用 PrintWriter 类 常用函数 返回值 方法名 说明 void write(int b) 将指定的字节转换成默认字符集的字符写出, 就是把int转换成char再写出。(int 类型是4个字节,而Write方法只写入int的最低八位,前面的3个字节完全忽略) void println(int x) 把所有传入此方法的内容转换成字符串原样打印出去 (此方法参数有多种重载形式,这里只拿int来演示) PrintWriter:字符打印流。 构造函数参数: 字符串路径。 File对象。 字节输出流。 字符输出流。 自动刷新功能    自动刷新功能,PrintWriter、PrintStream都支持,下面用PrintWriter的其中一个构造函数来解释说明。 构造方法名 说明

Java Socket网络编程初级入门

偶尔善良 提交于 2019-11-29 12:55:22
事实上网络编程简单的理解就是两台计算机相互通讯数据而已,对于程序员而言,去掌握一种编程接口并使用一种编程模型相对就会显得简单的多了,Java SDK提供一些相对简单的Api来完成这些工作。Socket就是其中之一,对于Java而言,这些Api存在与java.net 这个包里面,因此只要导入这个包就可以准备网络编程了。   网络编程的基本模型就是客户机到服务器模型,简单的说就是两个进程之间相互通讯,然后其中一个必须提供一个固定的位置,而另一个则只需要知道这个固定的位置。并去建立两者之间的联系,然后完成数据的通讯就可以了,这里提供固定位置的通常称为服务器,而建立联系的通常叫做客户端,基于这个简单的模型,就可以进入网络编程啦。   Java对这个模型的支持有很多种Api,而这里我只想介绍有关Socket的编程接口,对于Java而言已经简化了Socket的编程接口。首先我们来讨论有关提供固定位置的服务方是如何建立的。Java提供了ServerSocket来对其进行支持.事实上当你创建该类的一个实力对象并提供一个端口资源你就建立了一个固定位置可以让其他计算机来访问你,ServerSocket server=new ServerSocket(6789);这里稍微要注意的是端口的分配必须是唯一的。因为端口是为了唯一标识每台计算机唯一服务的,另外端口号是从0~65535之间的

PrintWriter to append data if file exist

好久不见. 提交于 2019-11-29 12:03:33
I have a savegame file called mysave.sav and I want to add data to this file if the file already exists. If the file doesn't exists, I want to create the file and then add the data. Adding data works fine. But appending data overwrites existing data. I followed the instructions of axtavt here ( PrintWriter append method not appending ). String savestr = "mysave.sav"; File f = new File(savestr); PrintWriter out = new PrintWriter(savestr); if ( f.exists() && !f.isDirectory() ) { out = new PrintWriter(new FileOutputStream(new File(savestr), true)); out.append(mapstring); out.close(); } else { out

Is PrintWriter buffered?

非 Y 不嫁゛ 提交于 2019-11-29 07:18:45
I know that the PrintWriter is really good if we want to write formatted data, and I also know the use of BufferedWriter to improve IO performance. But I tried something like this, PrintWriter pw = new PrintWriter(System.out); pw.println("Statement 1"); pw.println("Statement 2"); //pw.flush(); I observed that when the flush method is commented there is no output, but when I uncomment it, I get the desired output. This is only possible if the PrintWriter is buffered. If so, then what is the point of wrapping a PrintWriter using a BufferedWriter and then writing it? Though the javadoc doesn't

error: unreported exception FileNotFoundException; must be caught or declared to be thrown

天涯浪子 提交于 2019-11-29 06:13:47
I'm trying to create a simple program that will output a string to a text file. Using code I found here, I have put together the following code: import java.io.*; public class Testing { public static void main(String[] args) { File file = new File ("file.txt"); file.getParentFile().mkdirs(); PrintWriter printWriter = new PrintWriter(file); printWriter.println ("hello"); printWriter.close(); } } J-grasp throws me the following error: ----jGRASP exec: javac -g Testing.java Testing.java:10: error: unreported exception FileNotFoundException; must be caught or declared to be thrown PrintWriter

BIO/NIO/AIO的区分

故事扮演 提交于 2019-11-29 01:43:26
BIO:同步阻塞IO(平常说的IO指的是BIO) NIO:同步非阻塞IO AIO:异步非阻塞IO io操作分为两部分,发起io请求,和io数据读写。 阻塞、非阻塞主要是针对线程发起io请求后,是否立即返回来定义的,立即返回称为非阻塞io,否则称为阻塞io。 同步、异步主要针对io数据读写来定义的,读写数据过程中不阻塞线程称为异步io,否则,称为同步io。 一、BIO 线程发起io请求后,一直阻塞(阻塞io),直到数据就绪后,用户线程将数据写入socket空间,或从socket空间读取数据(同步)。 JDK5之前, JDK的IO模式只有BIO(同步阻塞)。 问题:因为阻塞的存在, 需对每个请求开启一个线程. 过多的线程切换影响操作系统性能。 解决:使用线程池, 处理不过来的放入队列, 再处理不过来的会触发其他机制。 问题::超过线程池数量的请求需要等待。 1. 客户端 public class Client { final static String ADDRESS = "127.0.0.1"; final static int PORT = 8765; public static void main(String[] args) throws IOException { Socket socket = null; BufferedReader in = null;

Difference between JspWriter and PrintWriter in Java EE?

独自空忆成欢 提交于 2019-11-29 01:07:23
问题 For all you "duplicate" fanatics, there is a similar question on SO right here. The difference is that I paint a vivid example that I can not understand the output of. The documentation for JspWriter and PrintWriter says there are two differences: 1. JspWriter can throw exceptions, PrintWriter should not do so. 2. JspWriter uses a PrintWriter behind the scene, but since by default JSP pages are buffered, the PrintWriter won't be created until the buffer is flushed - whatever that mean in the

Java聊天小程序(多人)

陌路散爱 提交于 2019-11-29 00:05:00
初学Java,用到网络编程和多线程的相关知识编辑了一套聊天程序,仅供参考,多多包涵:这是服务器: package test; import javax.swing.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.HashMap; import java.util.Map; import java.util.concurrent.CopyOnWriteArrayList; public class 服务器 { public static void main(String[] args) { 服务器 ff = new 服务器(); try { ff.WindowsChat(); } catch (IOException e) { } } CopyOnWriteArrayList<Socket> socket = new CopyOnWriteArrayList<>(); String a;int q; BufferedReader h; String

Java Web学习(七)HttpServletResponse(客户端响应)

女生的网名这么多〃 提交于 2019-11-28 19:49:48
本文借鉴: 孤傲苍狼 (特此感谢!) 一、定义   HttpServletResponse对象代表 服务端的响应 ,通过这个对象提供的方法,可以向客户机输出数据。 二、常用方法 PS: 在JavaWeb开发中,只要是写URL地址,那么建议最好以 "/" 开头,也就是使用绝对路径的方式, 如果"/"是给服务器用的,则代表当前的web工程,如果"/"是给浏览器用的,则代表webapps目录。 PS: 也可以使用 request.getContextPath() 来代替 "/项目名称" 的方式,这样更加灵活。     /** * 1.向客户端(浏览器)发送数据 * 原理:Servlet程序向ServletOutputStream或PrintWriter对象中写入的数据将被Servlet引擎从response里面获取,Servlet引擎将这些数据当作响应消息的正文,然后再与响应状态行和各响应头组合后输出到客户端。 * PS:getOutputStream和getWriter这两个方法互相排斥,调用了其中的任何一个方法后,就不能再调用另一方法。 * PS:Serlvet的service方法结束后,Servlet引擎将检查getWriter或getOutputStream方法返回的输出流对象是否已经调用过close方法,如果没有,Servlet引擎将调用close方法关闭该输出流对象。 */