Creating a file and reading its content.
The folder where the file will be crated

 

package ch4_package;
import java.io.*;
public class Test_This {
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String file_name ="", str_text="";
Process pp = new Process();
try{
//System.out.println("---While Loop---");
System.out.print("Please Enter a File name: ");
file_name = br.readLine();
System.out.print("Now the text to be added : ");
str_text = br.readLine();
pp.process(file_name, str_text);
}//try
catch(NumberFormatException e)
{ System.out.println("data was blank");}
}
}
class Process {
public String file_name="";
File create_file;
String show_text= "";
public void process(String str1, String str2)
{
try {
File file = new File(str1);
boolean success = file.createNewFile();
if (success){
file_name= file.getName();
show_text = str2;
System.out.print( "file name " + file_name);
char buff[] = new char[show_text.length()];
show_text.getChars(0,show_text.length(),buff, 0);
FileWriter fr = new FileWriter(str1);
for (int i =0; i <buff.length; i++)
{
fr.write(buff[i]);
}
fr.close();
}
} catch (IOException e) { System.out.println("could not read");
}

}

}

note the file is added

Reassurance of creating a file and added texts
 

file_name= file.getName();
show_text = str2;
System.out.print( "file name " + file_name);

 

and also note that the text has been added.