| File to String in Java |
|
|
|
| Written by Charles | |||||
| Tuesday, 17 November 2009 08:52 | |||||
|
The following is the most direct way of converting a File to a String in Java. The String will have the platform default encoding. The code is HERE import java.io.*;
public class F2S { /** * Description of the Method * * @param file * The file to be turned into a String * @return The file as String encoded in the platform * default encoding */ public static String fileToString(String file) { String result = null; DataInputStream in = null; try { File f = new File(file); byte[] buffer = new byte[(int) f.length()]; in = new DataInputStream(new FileInputStream(f)); in.readFully(buffer); result = new String(buffer); } catch (IOException e) { throw new RuntimeException("IO problem in fileToString", e); } finally { try { in.close(); } catch (IOException e) { /* ignore it */ } } return result; } }
Only registered users can write comments!
Powered by !JoomlaComment 3.26
3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |
|||||
| Last Updated ( Tuesday, 17 November 2009 09:05 ) |



