var regex = /^([a-zA-Z0-9])+([\w_\.\-]([a-zA-Z0-9])*[a-zA-Z0-9])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
Simple code to demonstrate to Read and Write text file in Java.
package email;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class ReadAndWriteFile {
public static void main(String[] args) {
try {
String content = "This is the content to write into file";
File writeFile = new File("D:/Sample.txt");
// if file doesnt exists, then create it
if (!writeFile.exists()) {
writeFile.createNewFile();
}
/**
* When we pass true as parameter to FilwWriter
* then it appends the content in file instead of overwriting the content
*/
//FileWriter fw = new FileWriter(file.getAbsoluteFile(),true);
FileWriter fw = new FileWriter(writeFile.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
fw.close();
/**
* File reading logic
*/
File readFile = new File("D:/Sample.txt");
BufferedReader bufferedReader = new BufferedReader(new FileReader(readFile));
String line = "";
while((line = bufferedReader.readLine()) != null)
{
System.out.println("Read from file===>"+line+"\n");
}
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Comments
Post a Comment