Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include a text file in a Java program with NetBeans

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.

like image 917
shumaila Avatar asked Jan 30 '26 13:01

shumaila


1 Answers

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.

like image 79
David A Tarris Avatar answered Feb 01 '26 01:02

David A Tarris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!