Saturday, February 26, 2011

Read a file in Java

public static String readFile(){
        System.out.println("Reading Dummy file");
        StringBuilder contents = new StringBuilder();
        File aFile = new File("/opt/liferay/6.0.4/test.xml");
        try {
              BufferedReader input =  new BufferedReader(new FileReader(aFile));
              try {
                String line = null;
                while (( line = input.readLine()) != null){
                  contents.append(line);
                  contents.append(System.getProperty("line.separator"));
                }
              }
              finally {
                input.close();
              }
            }
            catch (IOException ex){
              ex.printStackTrace();
            }
           
    return contents.toString();

    }

No comments:

Post a Comment