File operations in Java Read file, write into file, Append file

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) {
}


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) {
}

Read more


wait for batch script to finish ran from java process

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.

Read more

Popular Posts

Enter your email address:

Buffs ...

Tags


Powered by WidgetsForFree