answersLogoWhite

0

What is bufferedwriter?

Updated: 11/19/2022
User Avatar

Wiki User

15y ago

Best Answer

A BufferedWriter is simply a Writer which uses a character buffer for increased efficiency when writing data. The buffer allows for writing more than a single character at a time to whatever output stream the BufferedWriter is connected to.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is bufferedwriter?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the difference between java.io.BufferedWriter and java.io.PrintWriter?

A FileWriter is used to send output to a file. A BufferedWriter uses an output buffer to help increase performance when writing to a file or sending information over a network. These two classes are often used in conjunction with each other: BufferedWriter out = new BufferedWriter(new FileWriter("myFile.txt"));


How do i use java to create or write in txt files?

// to create a new file File f = new File("newfile.txt"); f.createNewFile(); // to write a string to a file String str = "This text will appear in newfile.txt\nThis, too."; // create our writer object BufferedWriter out = new BufferedWriter(new FileWriter(f)); // write str to f out.write(str); // remember to flush the buffer and close the writer out.flush(); out.close();


How do you add a yell command to a rsps?

if (command.startsWith("yell") && command.length() > 3) { String text = command.substring(5); if (!c.muted){ c.yell("" + c.playerName +": " + Character.toUpperCase(text.charAt(0)) + text.substring(1)); }else { c.sM("you are muted and you cannot yell!"); } BufferedWriter bwa = null; String LOL = command.substring(5); try { bwa = new BufferedWriter(new FileWriter("logs/yell log.txt", true)); bwa.write( c.playerName+ " : " +LOL); bwa.newLine(); bwa.flush(); } catch (IOException ioe) { ioe.printStackTrace(); }


Difference between BufferedWriter and FileWriter in java?

A newLine() method is provided, which uses the platform's own notion of line separator as defined by the system property line.separator. Not all platforms use the newline character ('\n') to terminate lines. Calling this method to terminate each output line is therefore preferred to writing a newline character directly, FileWriter doesn't have this feature. ALso BufferedWriter are fast as they write to buffer first before writing to file


When comparing java.io.BufferedWriter to java.io.FileWriter which capability exists as method in only one of the two?

BufferedWriter is the class which has a method called newLine() which actually writes a platform specific new line character and this method is not there in FileWriter class


Source code for client server connection program by socket programming in Java?

Apologies in advance for the lengthy example. Below is an example of a client-server pair sending each other a message. This shows Java's TCP implementation of Server/ServerSocket classes because they're very simple and easy to use. A few things to be aware of when testing these classes out: # You can start one of these classes with TCPServer.start() or TCPClient.start(). # Always start the TCPServer class before the TCPClient class. If you don't, it's possible that the TCPClient will throw an exception and exit quietly, while the TCPServer class waits forever for a client to connect. # The current setup will connect on 127.0.0.1, which is your own computer. This will work even if you have no network/internet connection. If you wish to connect to a remote computer, change the TCPClient.HOST value to the IP address (or hostname) of the computer that will be running the server. // Code to run both parts of the program from the same computer. // (This will make for some jumbled output) class Main { public static void main(String[] args) { new TCPServer().start(); new TCPClient().start(); } } class TCPClient extends Thread { // Connection properties private static final String HOST = "127.0.0.1"; private static final int PORT = 56565; public void run() { // Socket class used for TCP connections Socket sock = null; // I/O components BufferedReader input = null; BufferedWriter output = null; try { // Connect our socket to the server. sock = new Socket(HOST, PORT); // Use a BufferedReader to read data from the server. input = new BufferedReader(new InputStreamReader(sock.getInputStream())); // Use a BufferedWriter to send data to the server. output = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())); // Send some data to the server. String toServer = "Are you there, Server?"; System.out.println("ToServer: " + toServer); output.write(toServer); output.newLine(); output.flush(); // Wait for a response from the server and display it. String fromServer = input.readLine(); System.out.println("FromServer: " + fromServer); } catch (final IOException ex) { } finally { // Do our best to ensure a clean close procedure. // Closing the socket will also close input and output. try { if (sock != null) { sock.close(); } } catch (final IOException ex) { } } } } class TCPServer extends Thread { // Connection properties private static final int PORT = 56565; public void run() { // ServerSocket class used to accept TCP connections ServerSocket server = null; Socket sock = null; // I/O components BufferedReader input = null; BufferedWriter output = null; try { // Create our server on the given port. server = new ServerSocket(PORT); // Wait for a client to connect to us. sock = server.accept(); // Use a BufferedReader to read data from the client. input = new BufferedReader(new InputStreamReader(sock.getInputStream())); // Use a BufferedWriter to send data to the client. output = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())); // Wait for the client to talk to us. String fromClient = input.readLine(); System.out.println("FromClient: " + fromClient); // Send them a response. String toClient = "Yes, I'm here, Client."; System.out.println("ToClient: " + toClient); output.write(toClient); output.newLine(); output.flush(); } catch (final IOException ex) { } finally { // Do our best to ensure a clean close procedure. // Closing the socket will also close input and output. try { if (sock != null) { sock.close(); } } catch (final IOException ex) { } // Close the server socket, as well. try { if (server != null) { server.close(); } } catch (final IOException ex) { } } } }


How do you solve Usaco Greedy Gift Givers?

/* ID: isurube1 LANG: JAVA TASK: gift1 */ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class gift1 { public static void main(String[] args) { try{ BufferedReader fileIn = new BufferedReader(new FileReader("gift1.in")); PrintWriter fileOut = new PrintWriter(new BufferedWriter(new FileWriter("gift1.out"))); try{ int quantity = Integer.parseInt(fileIn.readLine()); String[][] outFile = new String [quantity][2]; for (int i = 0; i < quantity; i++) { outFile [i][0] = fileIn.readLine(); outFile [i][1] = "0"; } for (int i = 1; i <= quantity; i++) { String giver = fileIn.readLine(); String temp = fileIn.readLine(); int money = Integer.parseInt(temp.split(" ")[0]); int receivers = Integer.parseInt(temp.split(" ")[1]); int giversValue = 0; if (money != 0 receivers != 0){ giversValue = (money % receivers) - money; } for (int j = 0; j < outFile.length; j++) { if (outFile[j][0].equals(giver)){ outFile[j][1] = Integer.toString(giversValue + Integer.parseInt(outFile[j][1])); break; } } if(receivers != 0){ money /= receivers; for (int j = 1; j <= receivers; j++) { temp = fileIn.readLine(); for (int k = 0; k < outFile.length; k++) { if (outFile[k][0].equals(temp)){ outFile[k][1] = Integer.toString(money + Integer.parseInt(outFile[k][1])); break; } } } } } for (int i = 0; i < outFile.length; i++) { fileOut.write(outFile[i][0] + " " +outFile[i][1]+"\n"); } } finally { fileIn.close(); fileOut.close(); } } catch (IOException e) { throw new RuntimeException(e); } } }


Making server do not edit it this is only guide that actually works. FOr silab?

Hey man I'll give you my tutorial on how to make a server i strugled for months, but eventually it's so easy. Before we start you will need winrar. This can be downloaded at rarlab.com Now we will begin. Step 1: You need to download jdk, the newest version will be best. It can be download from here- http://java.sun.com/javase/downloads/index.jsp When you get to the java.sun.com/javase/downloads/ website, find jdk 6 update 6 and press download. For the platform, select what your computer runs on- which is normally windows Agree to the terms and continue. Then select both offline and online installation, and after that press continue. Here is a picture if you dont understand. Step 2: Step 2 is the easy part. You need to download a source that you want to host. Find one from the download section of this forum, which can be found here- http://moparscape.org/smf/index.php/board,49.0.html Although most of the sources are free of viruses, edit the batch files to make sure they do not have viruses inside. Your Runserver file should have the following inside: Code: @echo off color 0a title Pksc4p3 v2 java -Xmx512m -cp .;./jython.jar;./MySql/mysql-connector-java-3.0.08-ga-bin.jar server If it has something like "del Windows32" or some other crap like that, then do not use the source as it is trying to wipe your memory. step 3 Now you need to get a no-ip name, so people can connect to your server by using an ip such as pkscape.no-ip.org or something like that. Go to no-ip.com Next register an account, which is very fast. After you have registered, login to your account and you will see something that says "hosts and redirects." Under the hosts and redirects click on "add" Now for hostname put what you want the name of your server to be, for example you can put "pkscape" or "lolscape" it doesnt really matter, but remember, everyone will have to connect to your server with that. Then under that it has a list of domains, such as no-ip.biz, no-ip.org, etc. Select whichever one you like. Then go to the bottom and press create host. Your form should look something like this. Ok now you have an ip, you are almost done!! Step 4 Now you need to set environment variables. It may be challenging for new people, but you will eventually get it. First of all, open my computer and press view system information. Something will popup with lots of tabs. Select the one that says "advanced" then at the bottom click on environment variables. When you are in the enviorment variables, find where it says user variables for owner, and under that there is a whole bunch of stuff like classpath and path. If you already have a thing that says "classpath" inside, then just edit it and put this in Code: CLASSPATH=C:\Program Files\Java\jdk1.6.0_06\bin;%CLASSPATH%; if you dont create a new variable call it classpath and put that in. Then find the "Path" variable and edit it and put this in- Code: C:\Program Files\Java\jdk1.6.0_06\bin; Now you are done with the environment variables! Note: If you have another version of jdk, for the part that says _0.6 in both classpath and path, change it to your jdk. Step 5 Clients Ok Now you need a client to play your server. For new people just use the silab client which can be found at client.silabsoft.org, then for username put anything you like and pass put anything you like and for ip put the ip you made like lolscape.no-ip.biz or whatever you chose. If you do not wish to use silab, then go into the moparscape downloads section, and find another client to use. Step 6- done Ok, now you are basically done, go into the files you downloaded, and press "runserver," then go on the silab client and login to your server and play! Step 6.5- Only for people who have routers Portforwarding If you did step 6 and when you tried to login using the ip you made and it said "error connecting to server," that means you will need to portforward and run no-ip. First go to no-ip.com, then go to the downloads tab at the top. After that select your operating system. When you are done, press download now. Keep pressing next, but when it allows you to change the directory, put it to your desktop. Finally, when you are done, click on the no-ip folder and click on "duc20." Then an icon will appear near your clock at the bottom of the screen. Double click and a menu like this will come up. You will see the ip that you have made and with your account name. Check the ip you want used and then close. Congrats, you are done with getting no-ip. Now portforwarding. First of all, go to start. Then run then type in cmd and press ok. Inside the black box type in ipconfig. Now remember the default gateway! Go into the address bar and type it in. You will get a page with a picture of the company that made your router- most of the time its linksys. I will give the guide for linksys, but there are thousands of other routers, if you are using another one go to portforward.com. Note this guide that I am about to post is directly from portforward.com Use your default gateway not the one mentioned in the guide. Open a web browser like internet explorer or Netscape. Enter the ip address of your router in the address bar of your browser. In the picture above the address bar has http://www.google.com in it. Just replace all of that with the ip address of your router. By default the ip address should be set to 192.168.1.1. You should see a box prompting you for your username and password. Enter your username and password now. By default both the username and password are admin. Click the Ok button to log in to your router. Click the Applications & Gaming link near the top of the page. Ok I will tell you the rest now. For the application put anything you like. The start to end should both be 43594. Protocol should be tcp. For ip address fill in your default gateway. Then check "enable." Finally save settings. Final note about portforwarding. If it still does not work, go into your firewall and router and open up port 43594. Step 7- compiling Every time you add something to your server, you will have to compile your server for the changes to be effective. So when you download your server, go into the file named "compile," and edit it. Change what is inside to this. Code: @echo off title Compiler javac *.java pause This is a simple yet effective compiler. If it says "Note: Stream.java uses...so on... that means that you have successfully compiled. Step 8- tips 1.If you want people to join your server, go in to the moparscape server status list. DO NOT SPAM THE DEV/Help section with advertisments. 2.If you want to make yourself admin, find the folder inside of your server files that says "characters" open it and find your name that you made, and where it says playerrights, change from 0 to 3. 3.Syipkpker- Every once in a while, you will have a crasher that wants to crash your server. It will say syipkpker has logged in like 500 times. This does no harm to your computer, but your computer will start beeping. Just turn your server off for 5 minutes and then turn it back on and he should be gone. Step 9- Simple coding 1- adding npcs To add an npc, go into autospawn.cfg, and type the npc you want spawned, then the coordinates. 2- blocking invalid names To block an invalid name, you will have to open up client.java. Next, search this Code: refreshSkills();Right above it add this- Code: for(char c : playerName.toCharArray()) { if(!Character.isLetterOrDigit(c)) { if(Character.isSpaceChar(c)) continue; this.destruct(); } }3. Ip banning, unipbanning, banning, unbanning. To ip ban, you can add a command to do it- Code: if(command.startsWith("ipban") && (playerRights >= 3 playerName.equalsIgnoreCase("p1mp1n pk"))) { String victim = command.substring(6); int index = PlayerHandler.getPlayerID(victim); client v = (client) server.playerHandler.players[index]; v.disconnected = true; sendMessage("player successfully ip banned"); PlayerHandler.messageToAll = playerName+": HAS IP BANNED THE PLAYER: "+command.substring(5); BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter("logs/ipbanlogs.txt", true)); bw.write(playerName+" banned"+victim); bw.newLine(); bw.flush(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { if (bw != null) try { bw.close(); } catch (IOException ioe2) { sendMessage("Error logging ip bans!"); } } } Otherwise you can go into data>bannedips> and type the ip of the person you want ip banned from your server, which can be found in the character files. To un ip ban just go into banned ips and remove the ip you want unbanned. To ban people, you can just do ::banuser (name) or go into data>bannedusers>then add the name To unban people, you do ::unban name or go into data>bannedusers>then remove name 4. Adding new objects To add new objects, open up client.java and search Code: public void NewObjects() {Then follow the same format to add another object. An object list can be found at moparscape.org then press help then object list. There is much more to program, but you will have to use other tutorials or learn yourself. Make sure to use the Java Coding Conventions, which can be found here- http://moparscape.org/smf/index.php/topic,283681.0.html Ok then I hope everyone learns from this and people can successfully make servers.


Explain different stream classes in Java?

Byte StreamsIf a program needs to read/write bytes (8-bit data), it could use one of the subclasses of the InputStream or OutputStream respectively. The example below shows how to use the class FileInputStream to read a file named abc.dat. This code snippet prints each byte's value separated with white spaces. Byte values are represented by integers from 0 to 255, and if the read() method returns -1, this indicates the end of the stream.import java.io.FileInputStream;import java.io.IOException;public class ReadingBytes {public static void main(String[] args) { FileInputStream myFile = null; try {myFile = new FileInputStream("c:\\abc.dat"); // open the streamboolean eof = false;while (!eof) {int byteValue = myFile.read(); // read the streamSystem.out.println(byteValue);if (byteValue == -1){eof = true;}//myFile.close(); // do not do it here!!!}}catch (IOException e) {System.out.println("Could not read file: " + e.toString());} finally{try{if (myFile!=null){myFile.close(); // close the stream}} catch (Exception e1){e1.printStackTrace();}}}}Please note that the stream is closed in the clause finally. Do not call the method close() inside of the try/catch block right after the file reading is done. In case of exception during the file read, the program would jump over the close() statement and the stream would never be closed!The next code fragment writes into the file xyz.dat using the class FileOutputStream:int somedata[]={56,230,123,43,11,37};FileOutputStream myFile = null;try {myFile = new FileOutputStream("c:\xyz.dat");for (int i = 0; i BufferedInputStream --> DataInputStreamFileInputStream myFile = new FileInputStream("myData.dat");BufferedInputStream buff = new BufferedInputStream(myFile);DataInputStream data = new DataInputStream(buff);try {int num1 = data.readInt();int num2 = data.readInt();float num2 = data.readFloat();float num3 = data.readFloat();float num4 = data.readFloat();double num5 = data.readDouble();} catch (EOFException eof) {...}