文件操作

来源:互联网 发布:mac一般什么时候发布 编辑:程序博客网 时间:2024/06/07 05:23
package com.swt.common.util;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;public class FileUtil {/** * 将数据写入文件 * @param registration */public static void writerDataFile(String registration){StringBuffer strbuffBuffer=new StringBuffer();strbuffBuffer.append("key=");File file=new File("dll/registercodeKey.dll");//判断文件是否存在if(!file.exists()){try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}try {FileWriter writer=new FileWriter(file,true);//true表示追加写入文件BufferedWriter bfwriter=new BufferedWriter(writer);bfwriter.write(strbuffBuffer.append(Encryption.encryptAndUncrypt(registration)).toString());bfwriter.close();writer.close();} catch (IOException e) {e.printStackTrace();}}/** * 判断文件是否存在 * @param filepath * @return */public static  boolean fileIsExists(String filepath){File file=new File(filepath);if(file.exists())return true;elsereturn false;}/** * 判断 文件目录是否存在  不存在创建目录 * @param mkdir * @param file * @param data */public static boolean catalogIsExists(String mkdir){File fileMkdir=new File(mkdir);if(!fileMkdir.exists() && !fileMkdir.isDirectory()){//创建目录fileMkdir.mkdir();return true;}return false;}/** * 创建文件 并且写入数据 * @param filePath * @param data * @return */public static boolean createFileAndWriterData(String filePath,String data){//StringBuffer strbuffBuffer=new StringBuffer();strbuffBuffer.append("key=");File file=new File(filePath);//判断文件是否存在if(!file.exists()){try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}try {FileWriter writer=new FileWriter(file,true);//true表示追加写入文件BufferedWriter bfwriter=new BufferedWriter(writer);bfwriter.write(strbuffBuffer.append(Encryption.encryptAndUncrypt(data)).toString());bfwriter.close();writer.close();return true;} catch (IOException e) {e.printStackTrace();}return false;}/*** *  * 输入流 * java.io.InputStream这个类将数据读取为原始字节所需要的方法 * 无参数的read 方法读取字节数 作为0-255的int 类型返回 流结束有返回-1表示 * public abstract int read() throws IOException * public int read(byte[] input) throws IOException * public int read(byte [] input ,int offset,int length) throws IOException * public long skip(long n) throws IOException * public int available()throws IOException * public void close()trhows IOException * @throws IOException  *  */public static void fileReader() throws IOException{//读取二进制文件File file=new File("D:/Documents/Desktop/a.jpg");FileInputStream in=new FileInputStream(file);BufferedInputStream buffInput=new BufferedInputStream(in);//获取整个文件字节int available=buffInput.available();byte btyes[]=new byte[available];//一次性读出 表示流也结束(注意使用available 结束时候返回0)while(buffInput.read(btyes)!=0){System.out.println(new String(btyes));}buffInput.close();in.close();}/** *  *输出流提供数据所需要的基本方法 *public abstract void write(int b) throws IOException *public void write(byte[] data)throws IOException *public void write(byte[] data,int offset,int length)throws IOException *public void flush throws IOException *public void close() throws IOException *  *  * 二进制文件读取 * @throws IOException */public static void fileCopy() throws IOException{//读取二进制文件File file=new File("D:/Documents/Desktop/a.jpg");FileInputStream in=new FileInputStream(file);BufferedInputStream buffInput=new BufferedInputStream(in);//写入另外一个文件File copy=new File("D:/Documents/Desktop/b.jpg");if(!copy.exists()){copy.createNewFile();}//写入文件FileOutputStream out=new FileOutputStream(copy);BufferedOutputStream bufferOut=new BufferedOutputStream(out);int count=0;//读取1024byte [] bytes=new byte[1024];int len=0;//循环读取while((len=buffInput.read(bytes))!=-1){//System.out.println(new String(bytes, count, len));bufferOut.write(bytes, count, len);}bufferOut.flush();bufferOut.close();out.close();buffInput.close();in.close();}public static void main(String[] args) {//boolean bool=createFileMkdir("file/abadadesssfad");////if(bool){////boolean b=createFileAndWriterData("file/abadadesssfad/registercodeKey.dll", "A58AXE8D5E8SDES68SES");////if(b){//System.out.println("写入数据成功...去看看吧");////}else{//System.out.println("写入数据失败 你瞧瞧吧。。。。");//}//}else{//System.out.println("穿件文件夹失败咯哦");//}//System.out.println(Encryption.encryptAndUncrypt("bb{fgfpgfppfp").equals("A58AXE8D5E8SDES68SES"));try {fileReader();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

0 0
原创粉丝点击