File operations in Java Read file, write into file, Append file
File operations involved majorly are reading it , writing into a file and appending it.
Reading from a File :
try {
BufferedReader in = new BufferedReader(new FileReader("infilename"));
String str;
while ((str = in.readLine()) != null) {
process(str);
}
in.close();
} catch (IOException e) {
}
BufferedReader in = new BufferedReader(new FileReader("infilename"));
String str;
while ((str = in.readLine()) != null) {
process(str);
}
in.close();
} catch (IOException e) {
}
Writing into a File :
try {
BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
out.write("aString");
out.close();
} catch (IOException e) {
}
Appending to a File :
try {
BufferedWriter out = new BufferedWriter(new FileWriter("filename", true));
out.write("aString");
out.close();
} catch (IOException e) {
}
Thursday, August 02, 2012
//
Labels:
Java
//
2
comments
//
Running batch scripts through java and wait for it to complete which internally calls java processes.
The below example show how can we wait for the script to complete running.
public static void main(String[] args) throws InterruptedException,
IOException {
System.out.println("Starting ");
Runtime runtime = Runtime.getRuntime();
String[] command = { "cmd.exe", "/C", "start /WAIT",
"C:\\Users\\IBM_ADMIN\\Desktop\\BVT31\\BVT31\\xFAST.bat",
"--cf", "IE_Configurations.properties", "--tc",
"scripts\\testCases" };
Process p = runtime
.exec(command, null, new File(tipBVTScriptsPath));
p.waitFor();
System.out.println(p.exitValue());
}
String[] command = { "cmd.exe", "/C", "start /WAIT",....
Start /WAIT is the main thing to be understood here which will wait for the launched batch script in the separate window to get completed.
Once all the actions in the batch are completed the window remains open.
Hence the main important point here is to have "exit" statement as the last statement in the batch script which runs as process.
The below example show how can we wait for the script to complete running.
public static void main(String[] args) throws InterruptedException,
IOException {
System.out.println("Starting ");
Runtime runtime = Runtime.getRuntime();
String[] command = { "cmd.exe", "/C", "start /WAIT",
"C:\\Users\\IBM_ADMIN\\Desktop\\BVT31\\BVT31\\xFAST.bat",
"--cf", "IE_Configurations.properties", "--tc",
"scripts\\testCases" };
Process p = runtime
.exec(command, null, new File(tipBVTScriptsPath));
p.waitFor();
System.out.println(p.exitValue());
}
Start /WAIT is the main thing to be understood here which will wait for the launched batch script in the separate window to get completed.
Once all the actions in the batch are completed the window remains open.
Hence the main important point here is to have "exit" statement as the last statement in the batch script which runs as process.
//
Labels:
Java
//
0
comments
//
Popular Posts
-
The best solution to know about these init levels is to understand the " man init " command output on Unix. There are basically 8...
-
How to Unlock BSNL 3G data card to use it with Airtel and Vodafone Model no : LW272 ? How to unlock BSNL 3G data card( Model no : LW272 )us...
-
How to transfer bike registration from one State to other (Karnataka to Andhra)?? Most of us having two wheelers purchased and registered in...
-
ఓం శ్రీ స్వామియే శరణం ఆయ్యప్ప!! Related posts : Trip to Sabarimala - Part 1 Trip to Sabarimala - Part 2 Ayappa Deeksha required things...
-
Following are some of interesting blogs I found till now ...please comment to add your blog here. Blogs in English : http://nitawriter.word...
Popular posts
- Airtel and vodafone GPRS settings for pocket PC phones
- Andhra 2 America
- Ayyappa Deeksha required things
- Blogs I watch !
- Captions for your bike
- DB2 FAQs
- Deepavali Vs The Goddes of sleep
- ETV - Dhee D2 D3
- Evolution of smoking in India Women
- How to make credit card payments?
- init 0, init 1, init 2 ..
- Java-J2EE interview preparation
- mCheck Application jar or jad download
- My SQL FAQs
- My Travelogues
- Old is blod - New is italic
- Online pay methids for credit cards
- Oracle FAQs
- Pilgrimages
- Smoking in Indian Women
- Technology Vs Humans
- Twitter feeds for all Telugu stars on single page.
- Unix best practices
- Unix FAQs