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 exc开发者_如何转开发eption 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.
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.
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.
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.
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.
精彩评论