fill a file inside a inner class - java

I'm quiet new to java and I'm not even sure if the title makes any sense.
Anyway how do I gain access to a file inside of onStatus?
I'm just trying to write into a file in that function so in the code below "T_file.length()" the "T_file" cannot be resolved.
public static void main (){
foo("file_name");
}
foo(String fileName)
{
try{
final File T_file = new File (fileName);
final FileWriter fw = new FileWriter(fileName);
final BufferedWriter bw = new BufferedWriter (fw);
}catch (IOException e){}
StatusListener listener = new StatusListener(){
public void onStatus(Status status){
if (T_file.length() > 100){
System.out.println ("I have access here");
}
}
}
}

"T_file.length()" the "T_file" cannot be resolved
This is because T_file is not in the scope of onStatus. You declare T_file in the foo method, but then create a new StatusListener in which you redefine the onStatus, but the compiler still see StatusListener as an other class.
A workaround would be to declare the variable globally in your class, then access it specifying the name of your Class.this.T_file. Here an example :
public class Foo {
File T_file;
public static void main (){
new Foo("file_name");
}
Foo(String fileName)
{
T_file = new File (fileName);
StatusListener listener = new StatusListener(){
public void onStatus(Status status){
if (Foo.this.T_file.length() > 100){
System.out.println ("I have access here");
}
}
};
}
}
This example would compile.
Notice that I added a ; at the end of the StatusListener, else it would not compile.

Related

How to use a Path object as a String

I'm looking to try and create a Java trivia application that reads the trivia from separate question files in a given folder. My idea was to use the run() method in the FileHandler class to set every text file in the folder into a dictionary and give them integer keys so that I could easily randomize the order at which they appear in the game. I found a simple chunk of code that is able to step through the folder and get the paths of every single file, but in the form a Path class. I need the paths (or just the names) in the form a String class. Because I need to later turn them into a file class (which excepts a String Constructor, not a Path). Here is the chunk of code that walks through the folder:
public class FileHandler implements Runnable{
static Map<Integer, Path> TriviaFiles; //idealy Map<Integer, String>
private int keyChoices = 0;
public FileHandler(){
TriviaFiles = new HashMap<Integer, Path>();
}
public void run(){
try {
Files.walk(Paths.get("/home/chris/JavaWorkspace/GameSpace/bin/TriviaQuestions")).forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
TriviaFiles.put(keyChoices, filePath);
keyChoices++;
System.out.println(filePath);
}
});
} catch (FileNotFoundException e) {
System.out.println("File not found for FileHandler");
} catch (IOException e ){
e.printStackTrace();
}
}
public static synchronized Path getNextValue(){
return TriviaFiles.get(2);
}
}
There is another class named TextHandler() which reads the individual txt files and turns them into questions. Here it is:
public class TextHandler {
private String A1, A2, A3, A4, question, answer;
//line = null;
public void determineQuestion(){
readFile("Question2.txt" /* in file que*/);
WindowComp.setQuestion(question);
WindowComp.setAnswers(A1,A2,A3,A4);
}
public void readFile(String toRead){
try{
File file = new File("/home/chris/JavaWorkspace/GameSpace/bin/TriviaQuestions",toRead);
System.out.println(file.getCanonicalPath());
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
question = br.readLine();
A1 = br.readLine();
A2 = br.readLine();
A3 = br.readLine();
A4 = br.readLine();
answer = br.readLine();
br.close();
}
catch(FileNotFoundException e){
System.out.println("file not found");
}
catch(IOException e){
System.out.println("error reading file");
}
}
}
There is stuff I didn't include in this TextHandler sample which is unimportant.
My idea was to use the determineQuestion() method to readFile(FileHandler.getNextQuestion).
I am just having trouble working around the Path to String discrepancy
Thanks a bunch.
You can simply use Path.toString() which returns full path as a String. But kindly note that if path is null this method can cause NullPointerException. To avoid this exception you can use String#valueOf instead.
public class Test {
public static void main(String[] args) throws NoSuchFieldException, SecurityException {
Path path = Paths.get("/my/test/folder/", "text.txt");
String str = path.toString();
// String str = String.valueOf(path); //This is Null Safe
System.out.println(str);
}
}
Output
\my\test\folder\text.txt

Java file Formatting proper order

I am writing a program which requires the opening of a file, adding to it, and then closing it. For some reason when I try to do so, I get a nullPointerException.
Here is what I have in the main class:
public static filer f = new filer();
and then:
f.addStuff("hi");
f.closeFile();
now here is what I have in the file class, which is where I think the problem is right now:
public class filer {
private static Formatter f;//to add to file
private static Scanner s; //to read file
public static File file;
private static boolean ftf = false;
public static void openFile() {
try{ //exception handling
file = new File("jibberish.txt");
//shows if file created
System.out.println("File created: "+ ftf);
// deletes file from the system
file.delete();
// delete() is invoked
System.out.println("delete() method is invoked");
// tries to create new file in the system
ftf = file.createNewFile();
// print
System.out.println("File created: "+ ftf);
Formatter f = new Formatter(file);
Scanner s = new Scanner(file);
FileReader r = new FileReader(file);
/*
f = new Formatter("jibberish.txt"); //will automatically create jibberish.txt if no exist
s = new Scanner("jibberish.txt");
*/ //don't us these because scanner will take type string, easier to clear all other way
}
catch(IOException ioe){
System.out.println("Trouble reading from the file: " + ioe.getMessage());
}
}
public static void addStuff(String toAdd){
f.format("%s ", toAdd);
System.out.printf("%s added", toAdd);
}
public static void closeFile(){ //need to do this to avoid errors
f.close();
s.close();
}
the rest of the class works, i have all the proper imports and stuff
oh and of course here is what comes out of the console:
File created: false
delete() method is invoked
File created: true
Exception in thread "main" java.lang.NullPointerException
at filer.addStuff(filer.java:48)
at transMain.main(transMain.java:40)
You get a NullPointerException because when you call the addStuff method, f has not been initialized and is therefore null. Calling a method on a reference to a null object will result in a NullPointerException.
The problem is with the following line in openFile. You are creating a new local variable called f which hides the field named f declared at class level :
Formatter f = new Formatter(file);
Change the above line as follows so that the class level field f is the one that is initialized in openFile:
f = new Formatter(file);

getMethod return null java

I wrote getMethod in the file MovieReader and if I print this method inside this file everything is working well.
import java.io.BufferedReader; // scanner
import java.io.FileReader;
public class MovieReader {
private static String text;
public static void main(String args[]) throws Exception {
FileReader file = new FileReader("C:/Users/krystian/Desktop/filmDateBaseProject/movies.txt");
BufferedReader reader = new BufferedReader(file);
text = "";
String line = reader.readLine();
while(line != null) {
text+= line +"\n";
line=reader.readLine();
}
reader.close();
System.out.println(getText()); // This method works
}
public static String getText() {
return text;
}
}
But when I'm trying to call this method from other file it's printing null
public class Userr{
public static void main(String args[]){
MovieReader user = new MovieReader();
System.out.println(user.getText());
}
}
Can you help me with it?
In the MovieReader class you load the file and fill the contents of text in the main() method. When you create a new MovieReader object, the main() method is not executed, so the text field is not initialized.
You can create a static loader method in MovieReader and move the code from main() to there, like this:
public static void loadMovieInfo() {
FileReader file = new FileReader("C:/Users/krystian/Desktop/filmDateBaseProject/movies.txt");
... // rest of the code
reader.close();
}
Just call this before trying to call getText():
MovieReader.loadMovieInfo();
System.out.println(MovieReader.getText());
If you want the file to be loaded and the content of text to be filled when the object is created, you can turn text into an instance variable and load the file info in the MovieReader constructor.
Example:
public class MovieReader {
private String text;
public MovieReader() {
FileReader file = new FileReader("C:/Users/krystian/Desktop/filmDateBaseProject/movies.txt");
BufferedReader reader = new BufferedReader(file);
this.text = "";
String line = reader.readLine();
while(line != null) {
this.text += line +"\n";
line=reader.readLine();
}
reader.close();
}
public String getText() {
return this.text;
}
}
Then this should work:
MovieReader user = new MovieReader();
System.out.println(user.getText());
Also, a couple of observations:
Static methods belong to the class (not to a particular object), and should be called with the name of the class:
MovieReader.getText()
You should use a StringBuilder (docs here) instead of String concatenation to fill the contents of the text variable.
Try this one.
import java.io.BufferedReader; // scanner
import java.io.FileReader;
public class MovieReader {
private static String text;
public static String getText() {
FileReader file = new FileReader("C:/Users/krystian/Desktop/filmDateBaseProject/movies.txt");
BufferedReader reader = new BufferedReader(file);
text = "";
String line = reader.readLine();
while(line != null) {
text+= line +"\n";
line=reader.readLine();
}
reader.close();
System.out.println(getText()); // This method works
return text;
}
}
public class Userr{
public static void main(String args[]){
MovieReader user = new MovieReader();
System.out.println(user.getText());
}
}
The fast and dirty fix: Call the MovieReader.main method.
The longer answer, how you should actually do it:
You probably come from a scripting background like python. What you did here was to create two scripts, basically, wrapped in classes. When you call java, you have one class as entry point, whose main method is called.
So you created one script that loads a file, and another script that reads it, and your expectation is that both main methods are called. You need to go back to design!
The better way would be to only have a minimal main() in MovieReader, and instead have a method like readMovies(), which the main() calls. Then have User.main() call that method as well, before calling getText().
Don't put all the logic in main
First of all, you should call static getText() method with class name.
MovieReader.getText()
Second, default value of static string:
It's initialized to null if you do nothing, as are all reference types.
As, you are not doing anything that's why the value of text is null.
Refer the following fixed code:
MovieReader class
public class MovieReader {
private static String text;
public static void main(String args[]) throws Exception {
FileReader file = new FileReader("C:/Users/krystian/Desktop/filmDateBaseProject/movies.txt");
BufferedReader reader = new BufferedReader(file);
text = "";
String line = reader.readLine();
while(line != null) {
text+= line +"\n";
line=reader.readLine();
}
reader.close();
System.out.println(getText()); // This method works
}
public static String getText() {
return text;
}
}
Userr class:
public class Userr{
public static void main(String args[]){
try {
MovieReader.main(null);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(MovieReader.getText());
}
}
Assuming that you are running the main() method of Userr class.
main() method and getText() method of the class MovieReader are independent of each other. If you are calling getText() method, it will return the value of text variable without any operations on it, cos operations of main() method [ of MovieReader class ] are not going to execute. That's why you are not getting intended result.
try to re factor your code as below.
public class Movie {
public static void main(String[] args) {
MovieReader user = new MovieReader();
String file = "C:/Users/krystian/Desktop/filmDateBaseProject/movies.txt";
System.out.println(user.getText(file));
}
}
and the MovieReader class,
class MovieReader {
private String text;
String getText(String fileName) {
FileReader file;
file = null;
try {
file = new FileReader(fileName);
BufferedReader reader = new BufferedReader(file);
text = "";
String line = reader.readLine();
while (line != null) {
text += line + "\n";
line = reader.readLine();
}
reader.close();
return text;
} catch (FileNotFoundException ex) {
Logger.getLogger(MovieReader.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MovieReader.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
file.close();
} catch (IOException ex) {
Logger.getLogger(MovieReader.class.getName()).log(Level.SEVERE, null, ex);
}
}
return null;
}
}
its always a good practice to use exception handling.

Reading a file after pressing a button

I have tried to create a program that lets you to choose a file,reads it and then prints the results out... This is what i have so far
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Bebras braces matcher");
JButton selectf = new JButton("Open file");
selectf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if(cmd.equals("Open file")) {
JFileChooser chooser =new JFileChooser();
FileFilter filefilter = new FileNameExtensionFilter("","txt");
chooser.setFileFilter(filefilter);
int returnVal = chooser.showOpenDialog(chooser);
if(returnVal==JFileChooser.APPROVE_OPTION) {
FileName=chooser.getSelectedFile().getName();
System.out.println(FileName);
}
}
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.add(selectf);
frame.setVisible(true);
}
public static void countChar() throws FileNotFoundException {
Scanner scan = null;
try {
scan = new Scanner(new File(FileName));
}
catch (FileNotFoundException e) {
}
while (scan.hasNext()) {
String character = scan.next();
int index =0;
char close = '}';
char open = '{';
while(index<character.length()) {
if(character.charAt(index)==close){
CloseCount++;
}
else if(character.charAt(index)==open) {
OpenCount++;
}
index++;
}
}
System.out.println("Opened: "+OpenCount+"Closed: "+CloseCount);
}
private static String FileName;
private static int CloseCount = 0;
private static int OpenCount = 0;
private static final long serialVersionUID = 7526472295622776147L;
}
And it runs okay,just doesn't do what it need to... How do I make "countChar" run? Because it doesn't print what i should..
I forgot to mention, if I call it after i print out the files name, I get this errro: "Unhandled exception type FileNotFoundException", I actually know really less about those things..
You're almost there! You're just printing out the filename instead of calling that method.
See this?
if(returnVal==JFileChooser.APPROVE_OPTION) {
FileName=chooser.getSelectedFile().getName();
System.out.println(FileName);
}
Instead of (or before or after, if you prefer) System.out.println(FileName);, just put countChar();.
you just assign file name in FileName instead of file path.
so you use this
if(returnVal==JFileChooser.APPROVE_OPTION) {
FileName=chooser.getSelectedFile().getAbsolutePath();
countChar();
because if file is in same directory where you project is then work but when selected file reside in different places then need to use absolutepath of selected file.

How to call instance method in static method

So what am I trying to do is to read a .txt file and add some record on it using eclipse. I set my resource which I named it as "fileName" as private and when I try to call it in main method, there is some error. Here is my code:
public class FileController {
private String fileName;
public FileController() {
}
public FileController(String fileName) {
fileName = "student.txt";
}
public void readLine() {
try {
FileReader fr = new FileReader(fileName);
Scanner sc = new Scanner(fr);
// read in the file line by line
while (sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
fr.close();
} catch (FileNotFoundException exception) {
System.out.println("The file " + fileName + " was not found.");
} catch (IOException exception) {
System.out.println(exception);
}
}
public void writeLine() {
try {
// create the PrintWriter
FileWriter fw = new FileWriter(fileName, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter outFile = new PrintWriter(bw);
// write value out to the file
outFile.println("Coke is nice");
outFile.println("Diet Coke is even better cos won't put on weight =)");
// close the file
outFile.close();
System.out.println("File created: " + fileName);
} catch (IOException exception) {
System.out.println(exception);
}
}
public static void main(String[] args) {
FileController fs = new FileController();
fs.readLine();
fs.writeLine();
}
}
Anybody can give me some clues? These codes keep giving me NullPointerException error. I know it's from the FileController fs = new FileController() that line, but I do not know how to call instance method in static method.
Try:
public FileController() {
fileName = "student.txt";
}
public FileController(String fileName) {
this.fileName = filename;
}
I think your constructor should look like this:
public FileController(String fileName) {
this.fileName = fileName;
}
And the no-arg constructor like this:
public FileController() {
this("student.txt");
}
FileController fs = new FileController();
should be
FileController fs = new FileController("fileName");
You should also edit you constructor like this.Since class variable fileName and parameter name in constructor has same name you must have to use "this" keyword for assignment .
public FileController(String fileName) {
this.fileName = fileName;
}
If the parameter name and the class variable name is different you can do this.
public FileController(String name) {
fileName = name;
}
You are not passing the name of the file in the constructor. You should pass a valid file name in there for your logic to work properly
You need to call your constructor with an argument, or your default constructor should provide a default filename, for instance, in your main:
FileController fs = new FileController("somefile.txt")
And your constructor needs to be changed to:
public FileController(String filename) {
this.fileName = filename;
}
And you could change your default constructor:
public FileController() {
this("someDefaultFile.txt");
}
Keep in mind that the later option only makes sense if there is a default file to look for, otherwise you should explicitly pass the name of the file.
Instance methods can be accessed in static methods using the object reference of the class whose method you are trying to invoke.
Hence using new keyword as you have rightly pointed out will resolve your issue.
Problem is by calling new FileController(); you didn't initialized fileName field.
You probably wanted your constructors look like this:
public FileController() {
this.fileName = "student.txt";
}
public FileController(String fileName) {
this.fileName = fileName;
}
Then will be calling new FileController(); legit.

Categories