Help with java program =/

Discussion in 'OT Technology' started by Zappy, Nov 5, 2007.

  1. Zappy

    Zappy New Member

    Joined:
    Dec 6, 2006
    Messages:
    512
    Likes Received:
    0
    Ok a year or so ago my friend made a program in QB that was basicly a chat room based off a text file on the dropbox(shared drive). I am trying to recreate this program in java so it runs MUCH FASTER and does not slow down the computers to snail mode.

    The problem.

    It works perfect on my computer, but on school computers it just does not work properly.
    I am using 2 seperate programs, 1 a reader, 2 a writer.

    They run at the same time so I am thinking the problem is the school comp does not want to access the same file as the other program so it just does not work.
    If I had both open and I try to write it does not write to the file which is the problem.(yes I am testing on a local drive first).

    Here is the code.
    Writer
    Code:
    import java.io.*;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class main 
    {
        private static String name, chat;
    	public static void main(String[] args) 
    	{
            
    
    	boolean exit=false;
    		
    	
    	InputStreamReader input = new InputStreamReader(System.in);
    	BufferedReader reader = new BufferedReader(input); 
    	
    	// read in user input 
    	System.out.println("Please enter your name");
    	
    	//start user input
    	try
    	{
    	name = reader.readLine(); 
    	}
    	catch(Exception e){}
    	System.out.println("Your name is: " + name); 
    	//end user input
    	do
    	{
    	//start user input
    	try
    	{
    	chat = name+": "+reader.readLine(); 
    	}
    	catch(Exception e){}
    	try 
    	{
    		BufferedWriter out = new BufferedWriter(new FileWriter("chat.txt"));
    	    out.write(chat);
    	    out.close();
    	} catch (IOException e) {}
    	//end user input
    	
    	}while(exit!=true);
    	}
    }  
    
    Reader

    Code:
    import java.io.*;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Scanner;
    
    
    public class reader 
    {
    	private static String name, chat;      
        public static void main(String[] args) 
        {
     		 boolean exit=false;	
     		 FileInputStream fin;
     		 String check="1", check1="";
      		 int x=0;
       		 do
       		 {
    			//reading chat.txt
    			try
    			{
    			    // Open an input stream
    			    fin = new FileInputStream ("chat.txt");
    			  
    			    // Read a line of text
    			    check1 = new DataInputStream(fin).readLine();
    			    if(check.equals(check1))
    			    {	    	
    			    } 		
    				else
    				{
    					check = check1;
    			    	System.out.println(check);
    			    }	    
    			   	fin.close();		
    			}
    			// Catches any error conditions
    			catch (IOException e)
    			{
    				System.err.println ("Unable to read from file");
    				System.exit(-1);
    			}
       		}while(exit!=true);
    	}
    }
    
    I am still new at this so there will probably be some errors that need to be addressed lol. oh well atleast my programming styles decent -.-
     

Share This Page