Wednesday, October 14, 2015

Working with files in Java

Really obvious things for a lot of developers - but I still want to keep them here. You can blame me for not keeping them in mind - but I still don't have an answer what for :)

Reading from file:

public void readFile() {
        String str;
        try {
            File file = new File("Full path to file here");
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

            while ((str = bufferedReader.readLine()) != null) {
                isTestHeaderString(str);
            }
            bufferedReader.close();
        } catch (IOException exception) {
            exception.toString();
        }
    }


Writing to file:

public void writeToFile() {
    try {
        new File("Path do directory if we need that").mkdir();
        File file = new File("Path & name for file");
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        writer.append("What we actually want to write goes here");
        writer.close();
   } catch (IOException exception) {
        exception.toString();
   }
}

I'm not adding here any synchronisation / multi threading things - might add them later.


Sunday, October 11, 2015

Executing OSX terminal commands from Java

Couple days ago I've faced a situation when I needed to execute a pipeline of commands in OSX terminal from Java code and return result into program itself. You may say that there's a lot of information in the Internet (as a lot of us would say) but after spending some time it went out that those solutions are a bit incomplete for me :) So I'll add some information for those who might need that.

Problem:
- execute command in OSX terminal from Java program and return result from terminal back to the program;

Solution:

public static void main(String[] args) 
                             throws IOException, InterruptedException {
   try {
        Runtime runtime = Runtime.getRuntime();
        String[] cmd = {"/bin/bash", "-c", "Your command goes here"};

        Process process = runtime.exec(cmd);

        BufferedReader input = 
           new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line;

        while ((line = input.readLine()) != null) {
                System.out.println(line);
        }

    } catch (Exception e) {
        System.out.println(e.toString());
        e.printStackTrace();
    }
}

And it's simply all. So You might want to create some kind of service class for that command creation and then simply pass it to runtime executor - it's up to You.

Before the end just one more important detail:

Problem:
- execute pipeline of commands (where we need to execute 2 or more) and only after that return a result back to the program

Solution:
- here I'll show an example of such pipeline:

"cd /Users/Projects/Test && git checkout master && git checkout -b master-test && git log"

So You can add as many of Your commands as You need just separate those with && and that will work.

That's probably all for that topic.