java

位置:IT落伍者 >> java >> 浏览文章

java 常用IO操作


发布日期:2020年12月17日
 
java 常用IO操作

package comsyjutil;

import javaioBufferedReader;

import javaioBufferedWriter;

import javaioByteArrayInputStream;

import javaioByteArrayOutputStream;

import javaioFile;

import javaioFileInputStream;

import javaioFileOutputStream;

import javaioFileReader;

import javaioFileWriter;

import javaioIOException;

import javaioInputStream;

import javaioInputStreamReader;

import javaioObjectInputStream;

import javaioObjectOutputStream;

import javaioOutputStream;

import javaioPrintWriter;

import javaioStringReader;

import javautilArrays;

/**

* <p>

* Title:IO工具类

* </p>

*

* <p>

* Description:常用的IO操作封装

* </p>

*

* <p>

* Copyright: 转载请注明出处<A >

</A> * </p>

*

* @author 孙钰佳

* @main <A >mailto:>

</A> * @date Jun :: PM

*/

public class IOUtil {

/**

* 缓沖区大小 MB

*/

private static final int BUFFER_SIZE = * ;

/**

*

* Description: 将输入流输出到输出流

*

* @param in

* 输入流

* @param out

* 输出流

* @param bufferSize

* 缓沖区大小

* @throws IOException

* @mail <A >mailto:>

</A> * @sinceJun :: PM

*/

public static void inOutStream(InputStream in OutputStream out

int bufferSize) throws IOException {

byte[] buffer = new byte[bufferSize];// 缓沖区

for (int bytesRead = ; (bytesRead = inread(buffer)) != ;) {

outwrite(buffer bytesRead);

Arraysfill(buffer (byte) );

}

}

package comsyjutil;

import javaioBufferedReader;

import javaioBufferedWriter;

import javaioByteArrayInputStream;

import javaioByteArrayOutputStream;

import javaioFile;

import javaioFileInputStream;

import javaioFileOutputStream;

import javaioFileReader;

import javaioFileWriter;

import javaioIOException;

import javaioInputStream;

import javaioInputStreamReader;

import javaioObjectInputStream;

import javaioObjectOutputStream;

import javaioOutputStream;

import javaioPrintWriter;

import javaioStringReader;

import javautilArrays;

/**

* <p>

* Title:IO工具类

* </p>

*

* <p>

* Description:常用的IO操作封装

* </p>

*

* <p>

* Copyright: 转载请注明出处

* </p>

*

* @author 孙钰佳

* @main

* @date Jun :: PM

*/

public class IOUtil {

/**

* 缓沖区大小 MB

*/

private static final int BUFFER_SIZE = * ;

/**

*

* Description: 将输入流输出到输出流

*

* @param in

* 输入流

* @param out

* 输出流

* @param bufferSize

* 缓沖区大小

* @throws IOException

* @mail

* @sinceJun :: PM

*/

public static void inOutStream(InputStream in OutputStream out

int bufferSize) throws IOException {

byte[] buffer = new byte[bufferSize];// 缓沖区

for (int bytesRead = ; (bytesRead = inread(buffer)) != ;) {

outwrite(buffer bytesRead);

Arraysfill(buffer (byte) );

}

}

view plaincopy to clipboardprint?

/**

*

* Description: 读取文件返回字节数组流

*

* @param file

* 文件

* @return 字节数组流

* @mail <A >mailto:>

</A> * @sinceJun :: PM

*/

public static ByteArrayOutputStream readFileToByteStream(File file)

throws IOException {

FileInputStream fis = null;

ByteArrayOutputStream bos = null;

try {

fis = new FileInputStream(file);

bos = new ByteArrayOutputStream();

inOutStream(fis bos BUFFER_SIZE);

} finally {

if (fis != null)

fisclose();

}

return bos;

}

/**

*

* Description:读取文件返回字节数组

*

* @param file

* 文件

* @return 字节数组

* @throws IOException

* @mail <A >mailto:>

</A> * @sinceJun :: PM

*/

public static byte[] readFileToByteArray(File file) throws IOException {

ByteArrayOutputStream bos = null;

try {

bos = readFileToByteStream(file);

} finally {

if (bos != null)

bosclose();

}

return bostoByteArray();

}

/**

*

* Description:读取文件内容

*

* @param file

* 文件

* @return String内容

* @throws IOException

* @mail <A >mailto:>

</A> * @sinceJun :: PM

*/

public static String readFileToString(File file) throws IOException {

StringBuffer sb = null;

BufferedReader in = null;

try {

in = new BufferedReader(new FileReader(file));

sb = new StringBuffer();

for (String line; (line = inreadLine()) != null;) {

sbappend(line + \r\n);

}

} finally {

if (in != null)

inclose();

}

return sbtoString();

}

/**

*

* Description:复制文件

*

* @param src

* 源文件

* @param dest

* 目标文件

* @param cover

* 是否覆盖

* @throws IOException

* @mail <A >mailto:>

</A> * @sinceJun :: PM

*/

public static void copyFile(File src File dest boolean cover)

throws IOException {

/**

*

* Description: 读取文件返回字节数组流

*

* @param file

* 文件

* @return 字节数组流

* @mail

* @sinceJun :: PM

*/

public static ByteArrayOutputStream readFileToByteStream(File file)

throws IOException {

FileInputStream fis = null;

ByteArrayOutputStream bos = null;

try {

fis = new FileInputStream(file);

bos = new ByteArrayOutputStream();

inOutStream(fis bos BUFFER_SIZE);

} finally {

if (fis != null)

fisclose();

}

return bos;

}

/**

*

* Description:读取文件返回字节数组

*

* @param file

* 文件

* @return 字节数组

* @throws IOException

* @mail

* @sinceJun :: PM

*/

public static byte[] readFileToByteArray(File file) throws IOException {

ByteArrayOutputStream bos = null;

try {

bos = readFileToByteStream(file);

} finally {

if (bos != null)

bosclose();

}

return bostoByteArray();

}

/**

*

* Description:读取文件内容

*

* @param file

* 文件

* @return String内容

* @throws IOException

* @mail

* @sinceJun :: PM

*/

public static String readFileToString(File file) throws IOException {

StringBuffer sb = null;

BufferedReader in = null;

try {

in = new BufferedReader(new FileReader(file));

sb = new StringBuffer();

for (String line; (line = inreadLine()) != null;) {

sbappend(line + \r\n);

}

} finally {

if (in != null)

inclose();

}

return sbtoString();

}

/**

*

* Description:复制文件

*

* @param src

* 源文件

* @param dest

* 目标文件

* @param cover

* 是否覆盖

* @throws IOException

* @mail

* @sinceJun :: PM

*/

public static void copyFile(File src File dest boolean cover)

throws IOException {

view plaincopy to clipboardprint?

FileInputStream in = null;

FileOutputStream out = null;

try {

if (!destexists()) {

destcreateNewFile();

} else if (destexists() && cover) {

destdelete();

destcreateNewFile();

} else {

return;

}

in = new FileInputStream(src);

out = new FileOutputStream(dest);

inOutStream(in out BUFFER_SIZE);

} finally {

try {

if (in != null)

inclose();

} finally {

if (out != null)

outclose();

}

}

}

/**

*

* Description:写文件

*

* @param file

* 文件

* @param str

* 内容

* @throws IOException

* @mail <A >mailto:>

</A> * @sinceJun :: PM

*/

public static void writeFile(File file String str) throws IOException {

PrintWriter out = null;

BufferedReader in = null;

try {

if (!fileexists())

filecreateNewFile();

in = new BufferedReader(new StringReader(str));

out = new PrintWriter(new BufferedWriter(new FileWriter(file)));

for (String line; (line = inreadLine()) != null;) {

outprintln(line);

}

} finally {

try {

if (in != null)

inclose();

} finally {

if (out != null)

outclose();

}

}

}

/**

*

* Description:从控制台读取一串字符串

*

* @return 读取的字符串

* @throws IOException

* @mail <A >mailto:>

</A> * @sinceJun :: PM

*/

public static String readStringFromSystemIn() throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(Systemin));

try {

return brreadLine();

} finally {

if (br != null)

brclose();

}

}

/**

*

* Description:当ObjectInputStream对象调用

* readObject();时会从ByteArrayInputStream流中反序列化出的对象

*

*

* @param bi

* @return

* @throws IOException

* @mail <A >mailto:>

</A> * @sinceJun :: PM

*/

public static ObjectInputStream buildObjectInputStream(

ByteArrayInputStream bi) throws IOException {

return new ObjectInputStream(bi);

}

/**

*

* Description:当ObjectOutputStream对象调用

* writeObject(o);时o对象会序列化到ByteArrayOutputStream流中去

*

* @param bos

* 字节数组流

* @return 对象输出流

* @throws IOException

* @mail <A >mailto:>

</A> * @sinceJun :: PM

*/

public static ObjectOutputStream buildObjectOutputStream(

ByteArrayOutputStream bos) throws IOException {

return new ObjectOutputStream(bos);

}

public static BufferedReader buildBufferedReader(String str) {

return new BufferedReader(new StringReader(str));

}

public static ByteArrayInputStream buildByteArrayInputStream(String str) {

return new ByteArrayInputStream(strgetBytes());

}

public static ByteArrayInputStream buildByteArrayInputStream(byte[] bt) {

return new ByteArrayInputStream(bt);

}

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

byte[] bootFileBytes = IOUtilreadFileToByteArray(new File(

C:\\bootini));

Systemoutprintln(new String(bootFileBytes));

String bootFileStr = readFileToString(new File(C:\\bootini));

Systemoutprintln(bootFileStr);

Systemoutprintln(new String(bootFileBytes)equals(bootFileStr));

pyFile(new File(C:\\bootini) new File(C:\\bootini)

true);

IOUtilwriteFile(new File(C:\\bootini) bootFileStr);

ByteArrayOutputStream bos = new ByteArrayOutputStream();

ObjectOutputStream oos = IOUtilbuildObjectOutputStream(bos);

ooswriteObject(new String(abcd));

ObjectInputStream ois = IOUtilbuildObjectInputStream(IOUtil

buildByteArrayInputStream(bostoByteArray()));

Systemoutprintln(oisreadObject());

Systemoutprintln(IOUtilreadStringFromSystemIn());

}

}

               

上一篇:Java keytool工具的作用及使用方法

下一篇:Jjava中文汉字排序