Generate JSON File from a given YAML file I'm didn't use java for a long time(5 years ± ) and in the following code the  user should provide yaml file and it converting it to json file.

The code is working as expected but since I didn't use java for  a long time I want to see if I can write this code better and learn new things :-) in the latest java (currently with use of java8 not 9) 

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicReference;


public class yaml2json {


    public static void main(String[] args) {


        //Get File Path
        String sourceFilePath = getSourceFilePath(args);

        //Load File content
        AtomicReference&lt;String&gt; content = new AtomicReference&lt;String&gt;();
        try {
            content.set(readYamlFile(sourceFilePath));
        } catch (IOException e) {
            e.printStackTrace();
        }

        //Json string
        String jsonStr = null;
        try {
            jsonStr = convertYamlToJson(content.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

        //Print json file
        sourceFilePath = sourceFilePath.substring(0, sourceFilePath.length() - 3);
        sourceFilePath = sourceFilePath + "json";

        try {
            FileWriter fileWriter = new FileWriter(sourceFilePath);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(jsonStr);
            bufferedWriter.close();
            System.out.println("Success " + sourceFilePath);
        } catch (IOException err) {
            System.out.println("Error writing to file: " + err );
        }

    }

     private static String readYamlFile(String pathname) throws IOException {

        File file = new File(pathname);
        StringBuilder fileContents = new StringBuilder((int)file.length());
        Scanner scanner = new Scanner(file);
        String lineSeparator = System.getProperty("line.separator");

        try {
            while(scanner.hasNextLine()) {
                fileContents.append(scanner.nextLine() + lineSeparator);
            }
            return fileContents.toString();
        } finally {
            scanner.close();
        }
    }


    private static String getSourceFilePath(String[] args) {
        String inFile;
        if (args.length != 0) {
            inFile   = args[0];
        }else {
            inFile = "/Users/rayn/IdeaProjects/myproj/a.yml";

        }
        return inFile;
    }


    private static String convertYamlToJson(String yaml) throws IOException {

        ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
        Object obj = yamlReader.readValue(yaml, Object.class);

        ObjectMapper jsonWriter = new ObjectMapper();
        return jsonWriter.writeValueAsString(obj);
    }


}
Simplify File Reading

Java 7 and 8 make I/O a lot simpler. You can simplify your reading of a String from your input file to this:

    private static String readYamlFile(String pathname) throws IOException {
        String lineSeparator = System.getProperty("line.separator");
        List&lt;String&gt; lines = Files.readAllLines(Paths.get(pathname));

        return lines.stream().collect(Collectors.joining(lineSeparator));
    }


Remove the first two Try...Catch block

Your first try...catch blocks catch IOException and then just print out the stack trace. This adds lots of uneccesarry lines for little benefit. If you add throws IOException to public static void main(String args[]) the code becomes clearer, such as:

 public static void main(String[] args) throws IOException {

    //Get File Path
    String sourceFilePath = getSourceFilePath(args);

    //Load File content
    AtomicReference&lt;String&gt; content = new AtomicReference&lt;String&gt;();
    content.set(readYamlFile(sourceFilePath));
    //... and so on


Remove the AtomicReference

The AtomicReference might be useful if the code were multi-threaded. But it's not, the content variable is local to main(). So it just needs to be a String and with the removal of the try..catch above it can be simplified to:

String content = readYamlFile(sourceFilePath);


Don't Use sourceFilePath for your outputFilePath

It's a misleading name, give the output file its own variable. You can also just do it in a single line:

String outputFilePath = sourceFilePath.substring(0, sourceFilePath.length() - 3) + 
"json";


Also - this assumes the extension is three characters, so won't work correctly with a .yaml file. You can use a library like apache-commons to do this in a more foolproof way.

String outputFilePath = FilenameUtils.removeExtension(sourceFilePath)
         + ".json";


Simplify File Writing With PrintWriter

Again later versions of Java make dealing with I/O more straight-forward. I've kept the try...catch block so you can dispay the error message. You can also use the try-with resource to auto-close your PrintWriter if anything goes wrong

    try (PrintWriter out = new PrintWriter(outputFilePath)) {
        out.print(jsonStr);
        System.out.println("Success " + sourceFilePath);
    } catch (IOException err) {
        System.out.println("Error writing to file: " + err );
    }