Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: I/O of text file

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    3
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I/O of text file

    Hi i have to make this program that will read a message from another text file and surround each occurence of an abbreviation with <> brackets. and the write the marked message to a different file. The program must beable to read from a text file that contains the abbreviations and a text file that contains the message. Could someone please tell me if I am on the right track?

        package abbreviationstest;
        import java.io.File;
        import java.io.FileInputStream;
        import java.io.FileOutputStream;
        import java.io.FileWriter;
        import java.util.Scanner;
        import java.io.FileNotFoundException;
        /**
        *
        * @author 420
        */
        public class abbreviationsTest
        {
        public static void main(String[] args)
        {
        String fileName = "abbreviations.txt";
        Scanner inputStream = null;
        System.out.println("The file " + fileName + " contains the following lines:");
        try
        {
        inputStream = new Scanner(new File(fileName));
        //messageInputStream = new Scanner(new File(fileName));
        }
        catch(FileNotFoundException e)
        {
        System.out.println("Error opening the file " + fileName);
        }
        while (inputStream.hasNextLine())
        {
        String line = inputStream.nextLine();
        System.out.println(line);
        }
        inputStream.close();
        String message = "message.txt";
        //FileOutputStream// messageOutputStream =
        try
        {
        FileOutputStream outputStream = new FileOutputStream(new String(message));
        System.out.println("The file " + message + " contains the following lines:");
        Scanner keyboard = new Scanner(new File(message));
        String newMessage = message;
        do
        {
        newMessage = keyboard.next();
        outputStream.toString();
        }
        while(newMessage.matches(message));
        {
        System.out.print(newMessage);
        }
        }
        catch(FileNotFoundException e1)
        {
        System.out.println("Error opening the file " + message);
        }
        }
        }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I/O of text file

    Please post your code properly indented. Follow Java's naming conventions and begin class names with capital letters.

    Do you have an example file you're using to test this with? If so, post that too.

    How do test for the existence of abbreviations?

    A FileOutputStream is not appropriate for writing text files. Try PrintWriter, perhaps with a BufferedWriter.

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    3
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I/O of text file

    Yes I have an example file of abbreviations and the message that uses these abbreviations
    package abbreviationstest;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.util.Scanner;
    import java.io.FileNotFoundException;
     
    /**
     *
     * @author 420
     */
     
    public class abbreviationsTest
    {
        public static void main(String[] args) 
        {
            String fileName = "abbreviations.txt";
            Scanner inputStream = null;
            System.out.println("The file " + fileName + " contains the following lines:");
     
            try
            {
                inputStream = new Scanner(new File(fileName));
                //messageInputStream = new Scanner(new File(fileName));
            }
            catch(FileNotFoundException e)
            {
                System.out.println("Error opening the file " + fileName);
            }
            while (inputStream.hasNextLine())
            {
                String line = inputStream.nextLine();
                System.out.println(line);
            }
            inputStream.close();
     
            String message = "message.txt";
            //FileOutputStream// messageOutputStream =
            try
            {
                FileOutputStream outputStream = new FileOutputStream(new String(message));
                System.out.println("The file " + message + " contains the following lines:");
                Scanner keyboard = new Scanner(new File(message)); 
     
                String newMessage = message;            
                do
                {
                    newMessage = keyboard.next();
                    outputStream.toString();
                }
                while(newMessage.matches(message));
                {
                    System.out.print(newMessage);
                }
            }
            catch(FileNotFoundException e1)
            {
                System.out.println("Error opening the file " + message);
            }
     
        }
    }
    Attached Files Attached Files

Similar Threads

  1. Java code Split text file into multiple text file
    By jayraj in forum What's Wrong With My Code?
    Replies: 26
    Last Post: April 19th, 2013, 06:14 AM
  2. How do I copy a simple text-file to a new text-file?
    By get703 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 5th, 2012, 10:59 PM
  3. Beginner I/O Help: Writing To a Text File At the End of Existing Lines of Text
    By BloomingNutria in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 28th, 2012, 03:03 PM
  4. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  5. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM