• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read and save into text file

 
draco enzo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have 2 numbers in my file
100 first line
200 second line

i want to write 2 methods one used to read the 2 values and store them into x and y variable using scanner class and whats the best way to use try and catch statment?
the other method saves the 2 values (x,Y) into the file using Printwriter
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Why don't you put something together and we'll help you out!

WP
 
draco enzo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io*;
import java.util.*;

public void loadLoc( String filename)
{
try {
File myFile = new File("C:/coodinates.txt");
Scanner sc = new Scanner(MyFile);
Int coord = sc.nextInt();

dont know how to code from here cant think of good logic yes am stupid x.x
how can i read 2 integers from text file while the first number in first line get saved in x variable and second line number saved in Y variable of
object for example coord1


}

}
catch (FileNotFoundException e) {






public void saveLoc

{

try {
File myFile = new File("C:/coodinates.txt");
PrintWriter pw = new PrintWriter(myFile);
pw.println(x);
pw.println(y);
}
catch (FileNotFoundException e) {



}
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read some basics of java.io package. It has many reader and writer classes

There is a method in Scanner class , named, findInLine(Pattern pattern). It can help you out. See through the api. And make habit of going through it. It will benefit you a lot
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!