printwriter

closing nested streams [duplicate]

人盡茶涼 提交于 2019-12-29 08:54:06
问题 This question already has answers here : Closed 8 years ago . 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? 回答1: When closing chained streams, you only need to

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

为君一笑 提交于 2019-12-28 06:32:05
问题 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

亡梦爱人 提交于 2019-12-28 06:31:51
问题 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:

Properties类 序列化和反序列化Object 、Print

穿精又带淫゛_ 提交于 2019-12-26 02:20:34
Summarize : Properties :是map的子类,存储String类型键值对的双列集合,它的后缀格式为 .properties 文件 只允许有键值对和注释(不能写中文), 一行 是 一个键值对 , 不能有任何符号 序列化流( Object O utputStream )和反序列化流( Object I utputStream ):对文件的 写和读   打印流 :分字符和字节打印流 最明显的特点 写什么就打印什么,原样输出,还有自动刷新和换行功能 Properties类 : 特点: ①: Hashtable 的子类,map集合中的方法都可用 ②:该集合没有泛型,键值必须是字符串String类型 ③:可以持久化的属性集,键值可存储在集合中,也可存储在持久化的设备上,键值的来源也可以是持久化的设备。 方法: load(InputStream) 把指定流所对应的文件中的数据,读取出来,保存到 Properties 集合中 store(OutputStream,comments) 把集合中的数据,保存到指定的流所对应的文件中,后面的参数代表对描述信息--一个注释,可以是空字符串 创建后缀名为 .properties 的文件 #this is a person #Tue Jun 18 11:25:42 CST 2019 age=123 name=zhangsan #this is

unable to append to file using printwriter in java

假装没事ソ 提交于 2019-12-25 03:58:31
问题 I have the following code to initialise a printwriter object-- /* This function is used to initialise the printwriter element so that it can begin the task of writing data into assignor.txt file... * */ public void startwriterassignor(String filename, boolean appendToFile) { //pw = null; try { if (appendToFile== true) { //If the file already exists, start writing at the end of it. pw = new PrintWriter(new FileWriter(filename, true)); } else { pw = new PrintWriter(new FileWriter(filename,

Unable to print in Bluej console using PrintWriter

假装没事ソ 提交于 2019-12-24 21:15:37
问题 Consider the following code- import java.io.*; public class test { public static void main(String[] args) { PrintWriter out= new PrintWriter(System.out); out.println(1); out.close(); } } I run it on bluej for the first time and get output 1 on console. On running it again i get no output at all and same is the case for any subsequent tries. Would love to know why this is happening. 回答1: Ok, the problem why this method only works once is, that the PrintWriter.close() method also closes the

Shouldn't a method that receives java.lang.Object as input also receive javax.servlet.jsp.JspWriter as input?

心已入冬 提交于 2019-12-24 09:25:19
问题 I wanted to consolidate two functions. After getting a viable solution, I decided to play with the code a bit further, and came up with this: package hu.flux.helper; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.io.Writer; import javax.servlet.jsp.JspWriter; import com.objectmentor.library.web.framework.mocks.*; // A holder for formatting data public class NameAndAddress

PrintWriter won't write

前提是你 提交于 2019-12-24 08:27:12
问题 I have the following problem. I programmed a simple Echo Server and Echo Client but the problem is that in the loop of the Server, where I read from the Buffered Reader, the programme stuck and it won't write. import java.net.*; import java.util.*; import java.io.*; public class SimpleServer { public static void main(String[] args) { System.out.println("Dies ist ein simpler Echo Server"); int port = 6000; try { ServerSocket server = new ServerSocket(port); //server.setSoTimeout(30000); System

How to replace a specific line in a file using Java?

我怕爱的太早我们不能终老 提交于 2019-12-22 13:07:24
问题 How do I write over a specific line in a text file using FileWriter and PrintWriter? I don't want to have to make a new file every time. Edit: Can I just cycle through the file, get the length of the String at the indicated line number, and then use that length to backspace once I get to that line (to delete the String), and write in the new data? public static void setVariable(int lineNumber, String data) { try { // Creates FileWriter. Append is on. FileWriter fw = new FileWriter("data.txt",

How to replace a specific line in a file using Java?

这一生的挚爱 提交于 2019-12-22 13:05:12
问题 How do I write over a specific line in a text file using FileWriter and PrintWriter? I don't want to have to make a new file every time. Edit: Can I just cycle through the file, get the length of the String at the indicated line number, and then use that length to backspace once I get to that line (to delete the String), and write in the new data? public static void setVariable(int lineNumber, String data) { try { // Creates FileWriter. Append is on. FileWriter fw = new FileWriter("data.txt",