printwriter

NullPointerException in PrintWriter[] array - Java

僤鯓⒐⒋嵵緔 提交于 2019-12-13 05:17:00
问题 I have a problem which i am trying to figure out for days now. Simple Code Overview(PseudoCode): 1 - When a user connects create a PrintWriter for him. 2 - Store the PrintWriter object into a PrintWriter[] array. 3 - Increment the number of users connected. What is my problem? The compiler throws a NullPointerException What caused the NPE? When the sendMessage() method is called by object created in the mainWindow class the PrintWriter array if empty, i don't know what is causing it to be

A simple java client server program

百般思念 提交于 2019-12-13 02:23:34
问题 So I did this client server program in java for my college mini project. Note that this is just a small module of a big project I'm working on. I need a string to be sent from the client to the server. The server will return back the string as it is back to the client. (The code will be modified later such that the string is processed before sending back). The client will send a string whenever needed to the server. Thus it means it is compulsory for the server to be running for indefinite

Using a PrintWriter and File Object to Write to an Output File

不羁岁月 提交于 2019-12-13 01:29:08
问题 I have a JFileChooser object used to get a data file from the user. What I need to do is create a File object and PrintWriter object so that I can write to a file named "output.txt" . The file should be written to the same directory from which the data file was retrieved from. So far I have tried: // Write to a text file` File file = new File ("output.txt"); PrintWriter printWriter = new PrintWriter (f); This snippet of code creates the output file, but I need to it be written to the same

Code running stuck at scanner.hasNextLine()

烈酒焚心 提交于 2019-12-12 19:07:32
问题 I try to make a little Server-Client connection. They both have a Scanner and a PrintWriter, and they are writing to each other using a Socket's input and output stream. Client.java: import java.io.IOException; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; public class Client { static ServerSocket serverSocket; static Socket socket; static PrintWriter printWriter; static Scanner scanner; public

使用PrintWriter完成写操作 ,实现简易记事本工具

拜拜、爱过 提交于 2019-12-12 18:14:15
package seday07; import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Scanner; /** * @author xingsir * 简易记事本工具 * 程序启动后,要求用户输入文件名,然后对该文件写操作。 用户在控制台输入的每行字符串都按行写入该文件。用户输入的为exit时,程序退出。 * 使用PrintWriter完成写操作,并且创建要求自行创建流连接 */ public class Test { public static void main(String[] args) throws IOException { Scanner scanner=new Scanner(System.in); System.out.println("请输入文件名:"); String name=scanner.nextLine();//例如输入:Notepad,敲回车 FileOutputStream fos=new FileOutputStream(name);//文件流,

In Java, can I consolidate two similar functions where uses JspWriter and the other PrintWriter?

走远了吗. 提交于 2019-12-12 10:47:48
问题 I have the following class, which as you will see has rather a rather redundant formatNameAndAddress method: package hu.flux.helper; import java.io.PrintWriter; import javax.servlet.jsp.JspWriter; // A holder for formatting data public class NameAndAddress { public String firstName; public String middleName; public String lastName; public String address1; public String address2; public String city; public String state; public String zip; // Print out the name and address. public void

writing an array of strings down a column of a csv file in java

流过昼夜 提交于 2019-12-11 10:28:23
问题 so what i am trying to accomplish is to write an array of data down each cell of the column for as many array elements that i have. i want i too look like this in excel: buyer | parts | price| quantity | comments tom | blah | 546 | 5 | blah | blah | 56 | 67 | my file has manyyyyy more columns than this but i wanted to give a brief example. what also creates my problem is that i dont want to print the next column after it on the last row that the writer is on once its done my array data set,

PrintWriter not printing out complete string

蓝咒 提交于 2019-12-11 03:08:27
问题 I have the following code FileWriter F = new FileWriter("out.txt"); PrintWriter H = new PrintWriter(F); H.print(split[split.length - 2]); H.print("END"); When I examine the txt however, the last text is NOT 'END', but part of a word in the string. It is "repa" When I do this FileWriter F = new FileWriter("out.txt"); PrintWriter H = new PrintWriter(F); System.out.print(split[split.length - 2]); The last bit of text I get is the number '49' - this is correct. It appears that the PrintWriter is

Create New Text Files with Different Names in Java

亡梦爱人 提交于 2019-12-10 22:13:57
问题 Every time the code is executed I want a new Text File to be created. The Text File should be called Person1. Next time code is executed the Text File should be called Person2. Then again the Text File should be called Person3. etc, etc.... At the moment, I'm able to create a Text File named "Person1" but can't create another Text File named "Person2". private int fileNumber = 1; fileNumber = fileNumber++; public static void main(String[] args) { try { FileWriter fw = new FileWriter("Person"

In case of PrintWriter why should I flush in the loop and not after the loop?

前提是你 提交于 2019-12-10 15:13:33
问题 I have a Server and Client in my little demo program, where I send some string data from the Client to the Server, and after that resending this data for the Client, which also writes it out to the console. I was confused by PrtintWriter's flush method, which is - according to the JAVA documentation, - flushes the stream. After some researching I'm getting familiar with the concept of autoflash: when the autoFlash parameter is true println, printf, or format methods will flush the output