Results 1 to 3 of 3
  1. #1
    Kartright is offline Member
    Join Date
    May 2013
    Rep Power

    Default Adding more lines to a bufferedwriter

    I just made a small pin generator for a game, but I'm having an issue with it. It seems to not write data correctly.
    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    public class RSPINGenerator {
    	public static void main(String args[]) {
    		Scanner in = new Scanner(;
    		Random gen = new Random();
    		int n1, n2, n3, n4;
    		String name;
    		System.out.println("Enter the account name: ");
    		name = in.nextLine();
    		n1 = gen.nextInt(9);
    		n2 = gen.nextInt(9);
    		n3 = gen.nextInt(9);
    		n4 = gen.nextInt(9);
    		System.out.println(name +"'s pin is: " + n1 + n2 + n3 + n4);
    	    BufferedWriter writer = null;
    	    try {
    	        writer = new BufferedWriter(new FileWriter("C:\\Users/Pablo/Documents/pingen/"+name+".txt"));
    	        writer.write(n1 + n2 + n3 + n4);
    	    } catch (IOException e) {
    	    } finally {
    	        if (writer != null) {
    	            try {
    	            } catch (IOException e) {
    And when it writes to the text file, it either looks like what you'd see if you opened a .class file in notepad, or it's just a single question mark.

  2. #2
    kalata is offline Member
    Join Date
    Aug 2011
    Rep Power

    Default Re: Adding more lines to a bufferedwriter

    Hi, write(int arg) will write the char corresponding to that int. You are passing sum of 4 integers, so you'll write the char corresponding to that sum.
    This should do the trick for you:
    Java Code:
    writer.write("" + n1 + n2 + n3 + n4);

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Adding more lines to a bufferedwriter

    All input/output is done in binary form and all files contain binary data. How that data is interpreted upon I/O is dependent upon the a priori knowledge of the data. If the form happens to be readable character strings (text) then so be it. If the number is simply an integer say 1234 then it will be written as a binary value of 1234. If you open it up in a text editor, the editor will try and interpret it as ascii characters (usually unsuccessfully). If you want to write text you need to convert it to text first. You can also use PrintWriter to send formatted text to an output stream. What follows is a concrete example.

    Put this is a class and run it. Change the destFile name to suit your system.

    Java Code:
    String destFile = "F:/mystery.txt";
    try {
    DataOutputStream fio = new DataOutputStream(new FileOutputStream(
            new File(destFile)));
    } catch (IOException e) {
    Now open it up in Notepad.

    Side note: If you want to get a four digit pin, you can just do the following:

    Java Code:
    int pin = (int)(Math.random()*10000); // generate number between 0 and 9999 inclusive
    String val = String.format("%04d",pin); // convert it to a String with padded zeros.
    Last edited by jim829; 06-07-2013 at 01:41 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Adding lines into the frame
    By oror84 in forum AWT / Swing
    Replies: 0
    Last Post: 03-21-2012, 09:02 AM
  2. Replies: 3
    Last Post: 10-25-2011, 06:29 PM
  3. BufferedWriter Problem
    By dailywalker in forum New To Java
    Replies: 3
    Last Post: 04-04-2011, 10:07 PM
  4. BufferedWriter
    By Karthika in forum New To Java
    Replies: 6
    Last Post: 04-22-2010, 09:23 AM
  5. Adding lines at start and end of the text file
    By phani532 in forum New To Java
    Replies: 5
    Last Post: 08-27-2008, 03:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts