printwriter

201521123088《Java程序设计》第12周学习总结

删除回忆录丶 提交于 2019-12-22 05:09:33
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容。 2. 书面作业 将Student对象(属性:int id, String name,int age,double grade)写入文件student.data、从文件读出显示。 1. 字符流与文本文件:使用 PrintWriter(写),BufferedReader(读) 1.1 生成的三个学生对象,使用PrintWriter的println方法写入student.txt,每行一个学生,学生的每个属性之间用|作为分隔。使用Scanner或者BufferedReader将student.txt的数据读出。(截图关键代码,出现学号) //201521123088 Student类: public void writeData(PrintWriter out) { out.println(id + "|" + name + "|" + age + "|" + grade ); } public void readData(Scanner in) { String line = in.nextLine(); String[] tokens = line.split("\\|"); id =Integer.parseInt(tokens[0]); name =tokens[1]; age =

I've created a Java server with sockets, just how do print to ALL sockets?

…衆ロ難τιáo~ 提交于 2019-12-21 12:03:19
问题 I've been trying this for a while, and I want multiple clients to recieve multiple inputs simultaneously. There is one problem, I want the server to print "Hi" to all clients if one client says 'print2all Hi'. I know how to process it to print it, just to print to ALL clients is the problem. Here's what I have so far. Server try{ try{ server = new ServerSocket(25565); } catch (Exception e){ e.printStackTrace(); } while (isListening){ new SocketThread(server.accept()).start(); } server.close()

I created a PrintWriter with autoflush on; why isn't it autoflushing?

时光怂恿深爱的人放手 提交于 2019-12-21 04:22:09
问题 My client is a web browser, and sending request to myserver using this url: http://localhost This is the server side code. The problem lies in the run method of the ServingThread class. class ServingThread implements Runnable{ private Socket socket ; public ServingThread(Socket socket){ this.socket = socket ; System.out.println("Receives a new browser request from " + socket + "\n\n"); } public void run() { PrintWriter out = null ; try { String str = "" ; out = new PrintWriter( socket

Printing a replaced line in a new text file

只愿长相守 提交于 2019-12-20 07:21:19
问题 I am trying to edit a matlabfile and replace some of the coding parts init in some specific lines. However, using the format below to make the changes it wont change the line context at all. (it will print the same old line). Any idea what am I doing wrong? 'replaceAll' is not suitable to replace some words with some others in the lines? Thanks in advance. try { PrintWriter out = new PrintWriter(new FileWriter(filenew, true)); Scanner scanner = new Scanner(file); while (scanner.hasNextLine())

What if the difference between PrintWriter.print and PrintWriter.println?

给你一囗甜甜゛ 提交于 2019-12-20 07:09:01
问题 I have a server-client program set that I'm working on and right now I'm having problems with the protocol because the new line at the end of the println statement is confusing the metadata. In other words, I need to print to a PrintWriter without the new line at the end. I tried just print, but for some reason the other program doesn't get the message that way. I tried, for the sake of experimentation, adding "\n", "\r", and "\n\r" to the end of the statement and it still didn't get any data

PrintWriter autoflush puzzling logic

僤鯓⒐⒋嵵緔 提交于 2019-12-20 06:49:08
问题 public PrintWriter(OutputStream out, boolean autoFlush): out - An output stream autoFlush - A boolean; if true, the println, printf, or format methods will flush the output buffer public PrintStream(OutputStream out, boolean autoFlush): out - The output stream to which values and objects will be printed autoFlush - A boolean; if true, the output buffer will be flushed whenever a byte array is written, one of the println methods is invoked, or a newline character or byte ('\n') is written What

How do I achieve the following results using the PrinterWriter class from a text file?

Deadly 提交于 2019-12-20 06:38:38
问题 My application here prompts the user for a text file, mixed.txt which contains 12.2 Andrew 22 Simon Sophie 33.33 10 Fred 21.21 Hank Candice 12.2222 Next, the application is to PrintWrite to all text files namely result.txt and errorlog.txt . Each line from mixed.txt should begin with a number first followed by a name. However, certain lines may contain the other way round meaning to say name then followed by a number. Those which begins with a number shall be added to a sum variable and

Java - Understanding PrintWriter and need for flush

做~自己de王妃 提交于 2019-12-20 02:57:16
问题 Ok, firstly I apologise for all the code but I feel like too much code is better than not enough. I'm making a simple chat client and printwriter in particular i'm struggling with. With the code the way it is the now it will interact with the server class and perfectly fine and print what im wanting to print. However, when I remove 'writer.flush();' it will stop printing. With my understanding -which is evidently wrong- 'writer.println(outgoing.getText());' should be enough as this would send

JAVA Socket编程(二)之TCP通信

白昼怎懂夜的黑 提交于 2019-12-19 22:01:11
基于TCP(面向连接)的socket编程,分为客户端和服务器端。 客户端的流程如下: (1)创建套接字(socket) (2)向服务器发出连接请求(connect) (3)和服务器端进行通信(send/recv) (4)关闭套接字 服务器端的流程如下: (1)创建套接字(socket) (2)将套接字绑定到一个本地地址和端口上(bind) (3)将套接字设为监听模式,准备接收客户端请求(listen) (4)等待客户请求到来;当请求到来后,接受连接请求,返回一个新的对应于此次连接的套接字(accept) (5)用返回的套接字和客户端进行通信(send/recv) (6)返回,等待另一个客户请求。 (7)关闭套接字。 本文介绍一个多线程的TCP编程 1:创建服务器端并实现线程类 代码如下 1 package tcp; 2 3 import java.io.BufferedReader; 4 import java.io.InputStreamReader; 5 import java.io.PrintWriter; 6 import java.net.Socket; 7 8 public class Server implements Runnable {//实现一个线程类 9 private Socket soc; 10 public Server(Socket soc) { 11

Using PrintWriter and OutputStream

十年热恋 提交于 2019-12-19 08:01:06
问题 I am creating a project with struts and I have a problem using Jasper IReports. I want to export some info into a pdf file and I keep getting the java.lang.IllegalStateException: getOutputStream() has already been call... Exception due to openning a ServletOutputStream in my code when the page already opens a PrintWriter. The code is in the model (so it is not in the jsp, it's in a java file), as it follows: public void handle(HttpServletResponse res, Connection connection, String path)throws