0

Please see the Class file below called StringToFile. All I want to do is call a method inside StringToFile using another class file. Can anyone do this?

package StringToFile;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class StringToFile {
    public static void main(String[] args) throws IOException {
    }
    public static void write() throws IOException {
        String msg = "hello you\nthis is just a test\n";
        Files.write(Paths.get("./duke.txt"), msg.getBytes());
    }
    public static void read() throws IOException {
        List < String > lines = Files.readAllLines(Paths.get("./duke.txt"), Charset.defaultCharset());
        for (String line: lines) {
            System.out.println("line read: " + line);
        }
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
captainx
  • 25
  • 7
  • Communicating through the comments of someone else is pritty dumb, so I talk directly to you. I just read your second post about your problem and I got two simple methods that save and load a string to a .txt file. If you are interested, i could post them to help you out. – Aaron Priesterroth Feb 15 '16 at 19:11
  • Yes, that would be very helpful sir. Thank you! i would be most interested to see the code on how you are doing loading and saving a string. I also have a question. I have a java game that is running and it launches a mini game written in Visual Basic. The VB game writes to a txt file whether the player wins or loses the game. A "1" for win and a "0" for lose. the problem is both the main game and mini game run at same time. How would you let Java handle opening the txt file that the VB game wrote to when i dont know when the VB game is done and written its code string? – captainx Feb 17 '16 at 19:46

2 Answers2

0

If I'm understanding you correctly you want to import StringToFile in another class, and use it's static methods there. If that is the case, then in your other class you would import StringToFile.StringToFile, in order to gain visibility of this class. Then you would call it's static methods like normal via StringToFile.write() or StringToFile.read()

dustinroepsch
  • 1,122
  • 1
  • 7
  • 24
  • Just want to add that the calling method has to catch or rethrow the IOException. – Frank Puffer Feb 14 '16 at 21:12
  • That's the error I am getting. "error: unreported exception IOException; must be caught or declared to be thrown StringToFile.write(); <--- How do I fix this??? ^ – captainx Feb 14 '16 at 21:48
  • I used the following code to solve the IOException: try{ StringToFile.read(); }catch(IOException e){ //code to handle an IOException here } Now I get this: Error: Main method not found in class Altar, please define the main method as: public static void main(String[] args) Not sure what to do? – captainx Feb 14 '16 at 22:18
  • The way you handeled the exception is fine. The exception you are now getting is, that your IDE can't find the main method (the first method that is looked at after the program started). If you allready have a main method in another class, try selection the correct class before running your code. If you don't, add: public static void main(String[] args){ } as a method in your class. – Aaron Priesterroth Feb 14 '16 at 22:24
  • Hello Aaron. You are right. I have another class that is main. I am trying to make the class StringToFile run from another class with no luck. What do you mean when you say, "If you already have a main method in another class, try selection the correct class before running your code." <--- What do you mean select the correct class? What does the code look like for that? – captainx Feb 15 '16 at 07:21
  • Ok, I figured it out. Yay!. Now i have a new problem. LOL. I need to return the character that StringToFile.read() actually read from the text file to the class that originally called StringToFile. I can't seem to make that work now. LOL. – captainx Feb 15 '16 at 12:28
  • What you want to do now, is give your read method a return type. In your case this return type will surely be String. So change your method to public static String read(){} and at the end of the method, add "return stringName;" Obvioulsy you have to change stringName to the name of your String. Now to the method that calls the read method. To store the String simply type String string = StringToFile.read(); and it will be strored in string. Please remember to mark a solution on your question and give positive feedback. – Aaron Priesterroth Feb 15 '16 at 12:56
  • I got it working! Praise everyone here who helped me. Thank you Mr. Aaron Priesterroth. I am a beginner Java programmer who is actually working on a java game. It is my own project. It is really crazy what we are doing but just for laughs let me tell you. I needed this StringToFile to work because we are writing information to a text file using java because we need to share information between mini games written in C, Java or VB. LOL! It's a nightmare of course. haha. :) – captainx Feb 15 '16 at 17:50
0

Whenever you are using methods of another class, you create an instance of that class

ClassExample class = new ClassExample();

and call your method from that class

class.myMethod();

however if your class contains a static method, you can access it via the static modifier

ClassExample.myMethod();
Aaron Priesterroth
  • 307
  • 2
  • 4
  • 17