[1] string Filereaderstringhandle (String fileName)
Reads the file (specified by filename) into a string;
[2] byte[] Filereaderbytehandle (String fileName)
Reads a file (specified by filename) into a byte array;
[3] void Filewriterhandle (String fileName, string text)
Writes a string (specified by text) to a file (specified by filename).
Ioutil.java
import Java.io.BufferedInputStream;
import Java.io.BufferedReader;
import Java.io.File;
import Java.io.FileInputStream;
import Java.io.FileReader;
import java.io.IOException;
import Java.io.PrintWriter;
public class Ioutil {
/**
* Reads the file into a string, using Filereader+bufferedreader (provides ReadLine method)
*
* @param fileName
* @return String
*/
public static string Filereaderstringhandle (String fileName) {
StringBuilder sb = new StringBuilder ();
try {
BufferedReader in = new BufferedReader (new FileReader (New File (
fileName). Getabsolutefile ());
try {
String S;
while ((s = in.readline ())!= null) {
Sb.append (s);
sb.append ("\ n");
}
} finally {
In.close ();
}
} catch (IOException e) {
throw new RuntimeException (e);
}
return sb.tostring ();
}
/**
* Use Fileinputstream+bufferedinputstream to process files in byte
*
* @param fileName
* @return byte[]
*/
public static byte[] Filereaderbytehandle (String fileName) {
byte[] data = null;
try {
bufferedinputstream bf = new Bufferedinputstream (
new FileInputStream (FileName));
try {
data = new byte[bf.available ()];
bf.read (data);
} finally {
Bf.close ();
}
} catch (IOException e) {
throw new RuntimeException (e);
}
return data = null? New byte[] {}: Data;
}
/**
* Writes the specified text to a file with file name filename
*
* @param fileName
* @param text
*/
public static void Filewriterhandle (string fileName, string text) {
try {
PrintWriter out = new PrintWriter (new File (FileName)
. Getabsolutefile ());
try {
out.print (text);
} finally {
Out.close ();
}
} catch (IOException e) {
throw new RuntimeException (e);
}
}
public static void Main (string[] args) throws IOException {
System.out.print (Filereaderstringhandle ("Src/ioutil.java"));
for (Byte B:filereaderbytehandle ("Src/ioutil.java"))
System.out.print (b);
filewriterhandle ("Zj.txt",
filereaderstringhandle ("Src/ioutil.java"));
}
}
This article is from the "Zi Postman" blog, please be sure to keep this source http://zhangjunhd.blog.51cto.com/113473/74647