3

I want to include a test.txt file in my Java program, but I don't know how to include a file in NetBeans for a Java application.

This is my code:

package project;
import java.util.*;
import java.io.*;
import java.io.IOException;

public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException
    {
        new Main().run();
    }

    public void run()
    {
        System.out.println("Input String:\n");////
        Scanner keyboardScanner = new Scanner(System.in);/////
        String inString = keyboardScanner.nextLine();/////
        String shortMessage = shortifyMessage(inString);
        System.out.println(shortMessage);
    }

    public String shortifyMessage(String str)
    {
        String s = str;
        String tok1, tok2;
        try{
            FileInputStream fstream = new FileInputStream("textfile.txt");
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            String file_name = "C:/textfile.txt";
            try {
                textfile file = new textfile(file_name);
                String[] aryLines = file.OpenFile();
                int i;
                for (i=0; i<aryLines.length; i++){
                    System.out.println(aryLines[i]);
                }
            }
            catch (IOException e) {
            System.err.format("File Does not exist\n");
            }
            //Read File Line By Line
            while ((strLine = br.readLine()) != null)   {
                // Print the content on the console
                StringTokenizer tokenizer=new StringTokenizer(strLine,"=");
                tok1=tokenizer.nextToken();
                tok2=tokenizer.nextToken();

                //System.out.println(tok1 + " = " + tok2);

                if(s.indexOf(tok1)>0)
                    s = s.replaceAll(tok1, tok2);
                //System.out.println (s);
            }
            //Close the input stream
            in.close();
        }catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
        return s;
    }
}

When I run this code it gives the error:

Error: textfile.txt (The system cannot find the file specified)

I don't know how to include the text.txt file so that my code can read the contents of the file and perform appropriate functions.

1
  • 1
    Please, do not call a text file a notepad file. It's just like saying "IE File" or "Opera file" instead of HTML. Commented Jul 23, 2011 at 5:31

4 Answers 4

1

Sounds like you're trying to read in a text file and parse it. In which case you can read a file line by line like so:

File file = new File("test.txt");
Scanner in = new Scanner(file);
while (in.hasNextLine())
{
  String line = in.nextLine();
  System.out.println(line);
}

Since you're using NetBeans, it should tell you what you need to import and try-catch.

Just as a heads up if this is what you meant, "include" for a lot of programmers (especially those with a C++ background) typically implies you want to take source code from somewhere and "include" it as part of your program. If that's what you're trying to do, and your "test.txt" is actually Java code, you can put it in your local NetBeans project source directory and give it a ".java" extension.

Sign up to request clarification or add additional context in comments.

3 Comments

Most of the time, using File is a bad idea. Whenever the file is placed somewhere in the source tree, you should use getClass().getResource(....), so it works even when you move your source tree or create a JAR from it or whatever.
For beginners just learning how things work, File is appropriate. But +1 for a more professional way to go about this.
On the example there is an error File file = new File("test.txt"); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.println(line); }
0

How much have you learnt about file IO in Java so far?

You should import java.io.*; and java.util.scanner;

Then create a new Scanner for the file you want to read:

Scanner reader = new Scanner(new File("test.txt"));

Try going from there and writing your own code to make user of the scanner and read in the file.

1 Comment

I had imported java.io and java util.scanner.. but problem is still there, i have edited my question,please have a look.. Thank you.
0

Because your question is a little unclear, perhaps you mean to include this file in your project workspace? You can open a folder in Explorer and then drag-and-drop it into your folder in NetBeans.

Otherwise, these other answer should provide appropriate information for you.

Comments

0

You can just drag and drop the text file into the specific Java package you want in your folder in order to include the file in Netbeans.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.