What I'm trying to do is code a method that takes any kind of text input like
"words.text"
What I imagined it would look like would be
public static wordcount(File afile){....}
I want the method to be called such as
wordcount("words.txt");
I tried looking for the answer but couldn't find it. How do I do this?
Make the method with the following signature
public static void wordCount(String fileName){...}
Then inside the mthod use the string to make a File object.
public static void wordCount(String fileName){
File aFile = new File(fileName);
}
Related
In my program, I access files in a relative way many times in different classes.
And I do not want to create a private static final String PATH variable every time.
File file = new File(PATH);
How can I make all the paths to a separate file in java (in which I will indicate all the relative paths) so that I can access it from anywhere in the program.
You can try making an Enum and put all the paths in it.
something like
public enum Paths{
FILE1("path_to_file"),
FILE2("path_to_file")
}
Create a separate class Util.java and add all static variables as public inside that class.
For example
public class Util {
public static final String PATH_ONE = "C:\\location\\filepath1";
public static final String PATH_TWO = "C:\\location\\filepath2";
}
Access these paths from any classes like this
File file = new File(Util.PATH_ONE);
I solved my problem with a small method:
public static String getValue(String key) throws IOException {
Properties properties = new Properties();
properties.load(new FileInputStream("src\\main\\resources\\app.properties"));
return properties.getProperty(key);
}
Using JavaFX, I have the user input information into text fields.
My MainController class allows the user to save that inputted text to a txt file and save it at a given location.
Im wondering if its possible to pass what was entered in that text file into another class so I can parse and have my program use its data.
I used strings and .getText() then used a filewriter and bufferedWriter.
Can I get the .getText input into another class?
If your class is public than yes,you can use it in any class in your application;
You should research for Java - Access Modifiers
but as an ideia,
public class MainController {
private String savedFilePath;
public String getSavedFilePath() {
return savedFilePath;
}
public void setSavedFilePath(String savedFilePath) {
this.savedFilePath = savedFilePath;
}
}
and from the other class, when you save the file you can call:
MainController controller = new MainController();
controller.setSavedFilePath("file path");
If you don't want to create a new object MainController, you can define the savedFilePath as static
public class MainController {
private static String savedFilePath;
public static void setSavedFilePath(String paramSavedFilePath) {
savedFilePath = paramSavedFilePath;
}
}
and just call:
MainController.setSavedFilePath("file path");
I am currently dead in the water with a Java programming problem that seemed somewhat simple at first to do! I am trying to write text to a file from MULTIPLE methods in a class that does NOT contain a main() method, unlike other answers of this type question have used.
So... A quick outline of what my program is currently doing:
My program has one class (with the main() method obviously) that reads a text file stored on the disk, and passes sections of the text to certain methods in another class (second file in the project) to simply write the passed text to a text file. Each method in the class without the main() method needs to write the string passed to them to THE SAME file.
Why am I having trouble? I can easily write text to a file from ONE method in the class without the main() with FileWriter, but in order to have all of my other methods to write to the same file, I would need to make FileWriter global. I have tried to make it global, but then when I save text after another method saved text, it just rewrites the file to the latest text written.
My current class without the main() method:
package ADIFTOCSV;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class createADIF {
static File file;
static FileWriter fw;
static BufferedWriter writer;
static void init() throws IOException {
file = new File("/Users/Colin/Desktop/Myadif.txt");
fw = new FileWriter(file.getAbsoluteFile());
writer = new BufferedWriter(fw);
}
static void storeDate(String datez) throws IOException {
writer.write("<QSO_DATE:" + datez.length() + ">" + datez); <<----NULL POINTER EXCEPTION
}
static void storeFreq(String freqz) throws IOException {
writer.write("<FREQ:" + freqz.length() + ">" + freqz);
writer.close();
}
static void storeMode(String modez) {
}
static void storeBand(String bandz) {
}
static void storePower(String pwrz) {
}
static void storeTime(String timez) {
}
static void storeCall(String callz) {
}
static void storeRstSent(String rstsentz) {
}
static void storeRstRcvd(String rstrcvdz) {
}
static void storeComments(String commentsz) {
}
}
Each of these methods needs to write the String passed to them to the SAME file.
storeDate() is the first method to be called, therefore it writes text to the file first. However, when storeFreq() is called, it's text completely replaces the text written by storeDate(). This is obvious because I am forced to create a new FileWriter object in each method in order to write text. I cannot put FileWriter fw = new FileWriter(file.getAbsoluteFile()); outside the method to make it global; errors arise.
Am I missing something? Help is much appreciated. If any questions arise, or clarification is needed, please just ask!
You have to create the writer outside the methods.
Just defining the file outside is not enough.
If you recreate a writer to the same file in each method, of course it will overwrite.
The File instance is just a pointer to the file.
The writer is the "actual handle" that you need to reuse.
And be aware that you have to close the writer if you are finished with writing.
I would suggest that you scrap the class with the static methods and instead create a normal "File Write Handler" class which has a constructor where you can pass the File and writer to intialize the file writing classes and let that class handle all the writing to the file such that you can call a method like this:
FileWriteHandler.writer("<FREQ:" + freqz.length() + ">" + freqz);
and soforth for the rest you want printed. And finally call
FileWriteHandler.close();
Would be much cleaner and you could even make an interface for that class such that you can replace the FileWriterHandler with f.ex. a DatabaseWriteHandler or something like that.
I have two classes, one does something like this:
public ClassOne:
package classes;
public class ClassOne {
public javax.swing.JTextArea progressListing
progressListing = new javax.swing.JTextArea();
public void files(File file){
Class method = new Class();
method.methodInOtherClass(files);
}
public void progressUpdate (String fileOutput){
progressListing.insert(fileOutput,0);
}
}
which then goes to the other class that has the following:
Other Class:
package classes;
public class OtherClass extends ClassOne{
public void methodInOtherClass(file){
String fileOutput
fileOutput = file.getName();
ClassOne input = new ClassOne();
input.progressUpdate(fileOutput);
}
}
It is not updating the progessListing field when the program runs. Is there a better way to do this or am I missing something?
What OtherClass does is it creates pdf files that need to show up in the text area(ie the file path with the file name). ClassOne is the swing interface. Even when it's extended into the other class it doesn't modify the text field when I need it to.
Correct me if I'm wrong, but looking at the code you have posted, I think that you are trying to read a file and put the data from the file into the text area. Again, please correct me if I'm wrong. I think you should use the following code:
BufferedReader br = new BufferedReader(new FileReader("fileName.txt");
String data = br.readLine();
jTextArea1.append("\n"+data);
Please tell me if it works.
Cheers.
PS. If you post your whole code I will be able to help better.
I figured it out. I used a getter method in the other class to assign the variable and returned it to the main class. I set the String inside the loop to have an assign and add function. Works like a charm.
I am trying to write a big system that inputs data from a text file, and has a parser file. So, do I have to write a main file that would call the parser, and if so, how would I call the parser file, just code it like this?
Parser parser = new Parser();
If not, what would be my options???? Thank you for your help :)
FOR the parser how would I specify the text file to be read on the command line. I have a
public static void main (String[] commandLineArgs)
how would I write a specific text file in this statement???? Would i replace the commandLineArgs? I forgot about this.
YourClass YourFile; // Need to fully qualified class name and full path for the file.
EDIT : If I understand correctly, you need something like this :
public class Parser {
public void parseFile (String file) {
// parsing code goes here.
}
public static void main (String[] commandLineArgs) {
Parser parser = new Parser();
parser.parseFile(commandLineArgs[0]); //
}
}