FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Java bufferedReader problem





Dark-Tech
i was trying to create a program that would have a button that when you clicked it it would ask for the input of a name followed by the input of an url and then create a file with both of those it would also store the name in a list of all the names
now inorder to add a name to the list i was using

Code:
ArrayList PriorLines = new ArrayList();
      FileOutputStream out;
      PrintStream p;
        try
            {
                    FileInputStream fstream = new FileInputStream("Items.txt");
                    DataInputStream in = new DataInputStream(fstream);
                                 
       while (in.available() !=0)
                {
                     PriorLines.add(in.readLine());
                }
                in.close();
               
                    out = new FileOutputStream("Items.txt");
                    p = new PrintStream( out );
       for(int i =0; i<PriorLines.size(); i++)
                {
                p.println(PriorLines.get(i));
                }
                p.println(item);
                               
                p.close();
            }
         catch (Exception e)
            {
                System.err.println("File error");
            }



now my problem is it is supposed to be on the internet and that gives me an error of some kind and the button won't even show up on the webpage.

so i was trying to replace


Code:
  FileInputStream fstream = new FileInputStream("Items.txt");
                    DataInputStream in = new DataInputStream(fstream);


with

Code:
  BufferedReader in = new BufferedReader(new InputStreamReader(
                    new FileInputStream(new File("files/Items.txt"))));

but unfortunately BufferedReader doesn't have an available() method
used right aftter the declaration
Code:
 while (in.available() !=0)

if anyone could tell me the best way around this it would be appreciated and it may be an unrelated problem but why the button won't show up
Dark-Tech
here is the rest of the code incase it is of any importance to yuo helping me
Code:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;

public class MAINPROG extends Applet implements ActionListener
{

     TextField statusField;

     Button statusButton;

     public void init()
     {
          setLayout(new FlowLayout());
          statusButton = new Button("Save File");
          statusField = new TextField("Type item name here");
          statusButton.addActionListener(this);

          statusField.addActionListener(this);
          add(statusButton);
          add(statusField);
     }

     public void actionPerformed(ActionEvent act)
     {
          if (act.getSource() == statusButton)
           { 
              CreateItem();
           }
         
        }
       
       
       
       
       
       

public void CreateItem()
    {
       String item = JOptionPane.showInputDialog(null, "What is the name of the item you wish to add?");
      FileOutputStream out;
      PrintStream p;
  if (item != null){
      if (item.length() <= 0)
      {
          JOptionPane.showMessageDialog(null, "An Item was failed to be input.");
        }
        else
        {
        try
      {
        out = new FileOutputStream("files\\" + item + ".txt");
        p = new PrintStream( out );
         p.println(item);
         p.println(JOptionPane.showInputDialog(null, "What is the url for this item"));
         
          p.close();
          Additem(item);
        }
          catch (Exception e)
                {
                        JOptionPane.showMessageDialog(null, "Error writing to file");
                }           
               JOptionPane.showMessageDialog(null, "Item successfully added");
            }
    }
}
     public void Additem(String item)
    {
      ArrayList PriorLines = new ArrayList();
      FileOutputStream out;
      PrintStream p;
        try
            {
                BufferedReader in = new BufferedReader(new InputStreamReader(
                    new FileInputStream(new File("files/Items.txt"))));
     
                     PriorLines.add(in.readLine());
           
                in.close();
                    out = new FileOutputStream("files/Items.txt");
                    p = new PrintStream( out );
       for(int i =0; i<PriorLines.size(); i++)
                {
                p.println(PriorLines.get(i));
                }
                p.println(item);           
                p.close();
            }
         catch (Exception e)
            {
                System.err.println("File error");
            }
          }
        }
[/code]
poppitz
Hi,

you can use

Code:

String line = bufferedReader.readLine();

and
Code:

while (line != null){
  ...
  line = bufferedReader.readLine();
}
NG
theres a catgory for Java already ./
Related topics
Java Refresh Problem
problem z services.exe
Java Problem
Javascript problem...I think :S
just a java problem
Java classpath problem
Java vs. C++
problem with java script file
JAVA CLASSPATH
Problem with javax.sound.midi (Java)
Java/C++ helpers
embedding javascript menu simple problem
java applet refresh problem
Java runtime problem
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.