import java.io.*
i dont get the java.io.*
can anyone help me understand how to write to a file?
or better yet take the names in a file put them in an arraylist then add names from a keyboard?
right now i have
/**
* Write a description of class Try here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Try
{
// instance variables - replace the example below with your own
FileInputOutputClass io;
String inFile = "data.txt";
//String outFile ="data2.txt";
//
/**
* Constructor for objects of class Try
*/
public Try()
{
// initialise instance variables
{
io = new FileInputOutputClass();
io.readFile(inFile);
//io.readLine(inFile(2));
// io.writeFile(outFile);
}
}
and.................
/**
* Write a description of class FileInputOutput here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
public class FileInputOutputClass
{
/**
* Constructor for objects of class FileInputOutput
*/
public FileInputOutputClass()
{
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void readFile(String inFile )
{
BufferedReader in;
String justRead = null;
try
{
in = new BufferedReader (new FileReader(inFile));
while ((justRead = readLine(in)) != null)
{
System.out.println(justRead);
}
in.close();
}//end try
catch (FileNotFoundException e)
{
System.out.println("The file " + inFile + " was not found.");
return;
}
catch(IOException e)
{
System.out.println("Error reading from file " + inFile);
}
}
//-------------------------------------- ---------------------------------------- ------
public void writeFile(String outFile)
{
PrintWriter out;
String justRead = null;
try
{
out = new PrintWriter (new FileOutputStream (outFile));
}
catch(FileNotFoundException e)
{
System.out.println("The file "+ outFile +" was not found.");
return;
}
out.close();
}
//-------------------------------------- ---------------------------------------- --------
public String readLine(BufferedReader br)
{
String line = null;
try
{
line = br.readLine();
}
catch (IOException e)
{
readLine(br);
}
return line;
}
//--------------------------------end of FileInputOutput Class-------------------------
}
can anyone help me understand how to write to a file?
or better yet take the names in a file put them in an arraylist then add names from a keyboard?
right now i have
/**
* Write a description of class Try here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Try
{
// instance variables - replace the example below with your own
FileInputOutputClass io;
String inFile = "data.txt";
//String outFile ="data2.txt";
//
/**
* Constructor for objects of class Try
*/
public Try()
{
// initialise instance variables
{
io = new FileInputOutputClass();
io.readFile(inFile);
//io.readLine(inFile(2));
// io.writeFile(outFile);
}
}
and.................
/**
* Write a description of class FileInputOutput here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
public class FileInputOutputClass
{
/**
* Constructor for objects of class FileInputOutput
*/
public FileInputOutputClass()
{
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void readFile(String inFile )
{
BufferedReader in;
String justRead = null;
try
{
in = new BufferedReader (new FileReader(inFile));
while ((justRead = readLine(in)) != null)
{
System.out.println(justRead);
}
in.close();
}//end try
catch (FileNotFoundException e)
{
System.out.println("The file " + inFile + " was not found.");
return;
}
catch(IOException e)
{
System.out.println("Error reading from file " + inFile);
}
}
//--------------------------------------
public void writeFile(String outFile)
{
PrintWriter out;
String justRead = null;
try
{
out = new PrintWriter (new FileOutputStream (outFile));
}
catch(FileNotFoundException e)
{
System.out.println("The file "+ outFile +" was not found.");
return;
}
out.close();
}
//--------------------------------------
public String readLine(BufferedReader br)
{
String line = null;
try
{
line = br.readLine();
}
catch (IOException e)
{
readLine(br);
}
return line;
}
//--------------------------------end of FileInputOutput Class-------------------------
}
