Reputation: 21
I am new to java but I have looked for an answer but unfortunately haven't found one specific to my situation. I'd like to know where to place my file "icecream.txt" so that netbeans can read it. My programs works if I code the absolute the path but I'd prefer not to do that as it needs to run on the other student's computers without changes. I have attached an image of where I have placed my file. Any help would be appreciated. neatbeans file folders
My code is bellow if that helps
package icecreamsales;
/**
*
* @author anonymous
*/
public class IceCreamSales {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
TextIO.readFile("icecream.txt");
}
catch (IllegalArgumentException e) {
System.out.println("Can't open file \"icecream.txt\" for reading!");
System.out.println("Please make sure the file is present before");
System.out.println("running the program.");
System.exit(1); // Terminates the program.
}
int totalIceCreamSales = 0;
int strawberryIceCreamSales = 0;
while (!TextIO.eof()) {
String readLines = TextIO.getln();
totalIceCreamSales++;
if (readLines.equals("Strawberry")) {
strawberryIceCreamSales++;
}
}
System.out.println("Icecream cone sales totalled " + totalIceCreamSales);
System.out.println("Strawberry icecream sales totalled " + strawberryIceCreamSales);
System.out.println("Strawberry icecream is " + ((double) strawberryIceCreamSales/totalIceCreamSales*100) + "%%" + " of total sales");
}
}
Upvotes: 2
Views: 12392
Reputation: 1
First make a text file INSIDE Windows Explorer, make sure it's not in the package (NOT inside NetBeans). The IDE I used was NetBeans. The 'class name' and package must be named EXACTLY the same. If the class name isn't the same with package name it'll NOT work. Name package before using, and 'refactor' after right-clicking on package.
Readfile = where the input is being received and returned. Writefile = where the output is being returned and received.
package Project;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class Project {
public static void main(String[] args) {
String newReader = "read.txt";
String newWriter = "write.txt";
try {
FileReader reader = new FileReader(newReader);
BufferedReader in = new BufferedReader(reader);
FileWriter writer = new FileWriter(newWriter);
PrintWriter out = new PrintWriter(writer);
String line = null;
int numLines = 0;
while ((line = in.readLine()) != null) {
out.println(line.toUpperCase());
++numLines;
}
in.close();
out.close();
} catch (IOException e) {
System.out.println("ERROR!" + e);
}
}
}
C:\Users\abdul\OneDrive\Documents\NetBeansProjects\DadsProject
Make sure it's under the DadsProject (folder name), or under the folder of the name of the project you're working on.
C:\Users\abdul\OneDrive\Documents\NetBeansProjects\DadsProject\read * 'read' is the text file in this example
The file "read" is a text file make sure it has NO ".txt" attached as such when it's in 'WindowsExplorer'. You do NOT need the text file to be in 'NetBeans' just in 'WindowsExplorer' and that'll suffice. Make sure it's under 'DadsProject' and above 'src' (literally inside 'DadsProject' like so).
C:\Users\abdul\OneDrive\Documents\NetBeansProjects\DadsProject\src
Make sure it's above 'src' under 'DadsProject'
Make sure the file is .txt when it's in your code. For example:
String reader = "read.txt"; must have .txt or else it won't work
For example: Yes, to this => read (this is a text file) No, to this => read.txt (this is also a different text file) WARNING!!! read (text file) and read.txt (text file). They're NOT the same type of 'text file'.
Upvotes: 0
Reputation:
You need to move the file icecream.txt
into the netbeans project. For e.g.
IceCreamSales (Project)
|
+--icecream.txt
|
+--src
|
+--icecreamsales
|
+--IceCreamSales.java
Upvotes: 5
Reputation: 385
Just as @Stultuske said, you can place your text file anywhere. You can even dedicate a folder for future text files. You just have to know it's specific directory / location so that the "program" will know where to find those files needed.
Look at this answer for file directories references
Upvotes: 0