1.java读取文件内容到字符串
方法一:使用BuffererReader.继承Reader类
package readFail;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* 该方法用于获取文件中的数据
* @author fmz
*
*/
public class readFailDemo {
// E:/aaa/aaa.txt
public static void main(String[] args) {
try {
File file = new File("E:/aaa/aaa.txt");// 定义一个file对象,用来初始化FileReader
FileReader reader = new FileReader(file);// 定义一个fileReader对象,用来初始化BufferedReader
BufferedReader bReader = new BufferedReader(reader);// new一个BufferedReader对象,将文件内容读取到缓存
StringBuilder sb = new StringBuilder();// 定义一个字符串缓存,将字符串存放缓存中
String s = "";
while ((s = bReader.readLine()) != null) {// 逐行读取文件内容,不读取换行符和末尾的空格
sb.append(s + "\n");// 将读取的字符串添加换行符后累加存放在缓存中
System.out.println(s);
}
bReader.close();
String str = sb.toString();
System.out.println(str);
// File file = new File("E:/aaa/aaa.txt");
// if(!file.exists()){
// System.out.println("该文件不存在");
// }
// FileInputStream inputStream = new FileInputStream(file);
// int length = inputStream.available();
// byte bytes[] = new byte[length];
// inputStream.read(bytes);
// inputStream.close();
// String str =new String(bytes, StandardCharsets.UTF_8);
// System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法二: 使用FileInputStream类
package readFail;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* 该方法用于获取文件中的数据
* @author fmz
*
*/
public class readFailDemo {
// E:/aaa/aaa.txt
public static void main(String[] args) {
try {
// File file = new File("E:/aaa/aaa.txt");// 定义一个file对象,用来初始化FileReader
// FileReader reader = new FileReader(file);// 定义一个fileReader对象,用来初始化BufferedReader
// BufferedReader bReader = new BufferedReader(reader);// new一个BufferedReader对象,将文件内容读取到缓存
// StringBuilder sb = new StringBuilder();// 定义一个字符串缓存,将字符串存放缓存中
// String s = "";
// while ((s = bReader.readLine()) != null) {// 逐行读取文件内容,不读取换行符和末尾的空格
// sb.append(s + "\n");// 将读取的字符串添加换行符后累加存放在缓存中
// System.out.println(s);
// }
// bReader.close();
// String str = sb.toString();
// System.out.println(str);
File file = new File("E:/aaa/aaa.txt");
if(!file.exists()){
System.out.println("该文件不存在");
}
FileInputStream inputStream = new FileInputStream(file);
int length = inputStream.available();
byte bytes[] = new byte[length];
inputStream.read(bytes);
inputStream.close();
String str =new String(bytes, StandardCharsets.UTF_8);
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.将文件封装成流并以字符串String类型输出
package readFail;
import java.io.File;
import java.io.FileInputStream;
import Decoder.BASE64Encoder;
/**
* 方法描述:该方法是将文件封装成流并以字符串String类型输出
* 注意:
* 需要添加该jar包:sun.misc.BASE64Decoder.jar
* @author fmz
*
*/
public class beanStreamFail {
E:/aaa/RepairPlatform.apk
public static void main(String[] args) {
try {
String streamFail = encodeBase64File("E:/aaa/RepairPlatform.apk");
System.out.println(streamFail);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String encodeBase64File(String path) throws Exception {
File file = new File(path);
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int)file.length()];
inputFile.read(buffer);
inputFile.close();
return new BASE64Encoder().encode(buffer);
}
}
3.Android文件和base64互转(在项目中需要用到文件传输入,有时需要将文件转成base64字串,再将base64字串转成字节流保存在文件了)
/**
* 将文件转成base64 字符串
* @param path 文件路径
* @return
* @throws Exception
*/
public static String encodeBase64File(String path) throws Exception {
File file = new File(path);
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int)file.length()];
inputFile.read(buffer);
inputFile.close();
return new BASE64Encoder().encode(buffer);
}
/**
* 将base64字符解码保存文件
* @param base64Code
* @param targetPath
* @throws Exception
*/
public static void decoderBase64File(String base64Code,String targetPath) throws Exception {
byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
FileOutputStream out = new FileOutputStream(targetPath);
out.write(buffer);
out.close();
}
/**
* 将base64字符保存文本文件
* @param base64Code
* @param targetPath
* @throws Exception
*/
public static void toFile(String base64Code,String targetPath) throws Exception {
byte[] buffer = base64Code.getBytes();
FileOutputStream out = new FileOutputStream(targetPath);
out.write(buffer);
out.close();
}
4.Java读取txt文件和覆盖写入txt文件和追加写入txt
/**
* 创建文件
* @param fileName
* @return
*/
public static boolean createFile(File fileName)throws Exception{
try{
if(!fileName.exists()){
fileName.createNewFile();
}
}catch(Exception e){
e.printStackTrace();
}
return true;
}
/**
*读取TXT内容
* @param file
* @return
*/
public static String readTxtFile(File file){
String result = "";
try {
InputStreamReader reader = new InputStreamReader(new FileInputStream(file),"gbk");
BufferedReader br = new BufferedReader(reader);
String s = null;
while((s=br.readLine())!=null){
result = result + s;
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 写入TXT,覆盖原内容
* @param content
* @param fileName
* @return
* @throws Exception
*/
public static boolean writeTxtFile(String content,File fileName)throws Exception{
RandomAccessFile mm=null;
boolean flag=false;
FileOutputStream fileOutputStream=null;
try {
fileOutputStream = new FileOutputStream(fileName);
fileOutputStream.write(content.getBytes("gbk"));
fileOutputStream.close();
flag=true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
* 写入TXT,追加写入
* @param filePath
* @param content
*/
public static void fileChaseFW(String filePath, String content) {
try {
//构造函数中的第二个参数true表示以追加形式写文件
FileWriter fw = new FileWriter(filePath,true);//Java使用FileWriter实现文件的写入,用法为:FileWriter(file,true); 其中第二个参数设置成false就是覆盖写入,true就是增量存储。
fw.write(content);
fw.close();
} catch (IOException e) {
System.out.println("文件写入失败!" + e);
}
}
public static void main(String[] args) throws Exception{
File file = new File("D:\\123wu吴.txt");
createFile(file);
readTxtFile(file);
// writeTxtFile("我是写入的内容11",file);
fileChaseFW("D:\\123wu吴.txt","66666666");
}