小白求教!!C# 将二进制文件流转化为string并存放到一个变量里
1个回答
推荐于2016-08-07
展开全部
private static String bytesToString(String filename) {
String myreadline = "";
try {
// 创建FileReader对象,用来读取字符流
FileReader fr = new FileReader("userinfo.txt");
// 缓冲指定文件的输入
BufferedReader br = new BufferedReader(fr);
// 定义一个String类型的变量,用来每次读取一行
while (br.ready()) {
// 读取一行
myreadline = myreadline + (myreadline.equals("")?"":"\n") + br.readLine();
// 在屏幕上输出
System.out.println(myreadline);
}
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
return myreadline;
}
public static void writerFile(String fileString, String filePath) {
FileWriter fw;
try {
fw = new FileWriter(filePath);
// 将缓冲对文件的输出
BufferedWriter bw = new BufferedWriter(fw);
bw.write(fileString);
bw.flush();
bw.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws FileNotFoundException,
Exception {
// TODO Auto-generated method stub
String fileString = bytesToString("D:/workspace/TestStruts2/userinfo.txt");
writerFile(fileString,"D:/workspace/TestStruts2/test.txt");
}
String myreadline = "";
try {
// 创建FileReader对象,用来读取字符流
FileReader fr = new FileReader("userinfo.txt");
// 缓冲指定文件的输入
BufferedReader br = new BufferedReader(fr);
// 定义一个String类型的变量,用来每次读取一行
while (br.ready()) {
// 读取一行
myreadline = myreadline + (myreadline.equals("")?"":"\n") + br.readLine();
// 在屏幕上输出
System.out.println(myreadline);
}
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
return myreadline;
}
public static void writerFile(String fileString, String filePath) {
FileWriter fw;
try {
fw = new FileWriter(filePath);
// 将缓冲对文件的输出
BufferedWriter bw = new BufferedWriter(fw);
bw.write(fileString);
bw.flush();
bw.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws FileNotFoundException,
Exception {
// TODO Auto-generated method stub
String fileString = bytesToString("D:/workspace/TestStruts2/userinfo.txt");
writerFile(fileString,"D:/workspace/TestStruts2/test.txt");
}
追问
有办法用FileStream读取文件,并放在一个string类型的变量里吗?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询