如何用Java读取一个txt文件,并将文件内容保存到String类型的变量中?求Java代码!
3个回答
展开全部
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile {
public static void main(String[] args) throws IOException {
String fileContent = readFileContent("");
System.out.println(fileContent);
}
//参数string为你的文件名
private static String readFileContent(String fileName) throws IOException {
File file = new File(fileName);
BufferedReader bf = new BufferedReader(new FileReader(file));
String content = "";
StringBuilder sb = new StringBuilder();
while(content != null){
content = bf.readLine();
if(content == null){
break;
}
sb.append(content.trim());
}
bf.close();
return sb.toString();
}
}
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile {
public static void main(String[] args) throws IOException {
String fileContent = readFileContent("");
System.out.println(fileContent);
}
//参数string为你的文件名
private static String readFileContent(String fileName) throws IOException {
File file = new File(fileName);
BufferedReader bf = new BufferedReader(new FileReader(file));
String content = "";
StringBuilder sb = new StringBuilder();
while(content != null){
content = bf.readLine();
if(content == null){
break;
}
sb.append(content.trim());
}
bf.close();
return sb.toString();
}
}
TableDI
2024-07-18 广告
2024-07-18 广告
作为上海悉息信息科技有限公司的一员,我们专注于提供高效的数据处理解决方案。对于多个文件表格的合并需求,我们通常采用专业的数据整合技术,确保数据的准确性和一致性。通过精确匹配表格字段和格式,我们能够快速、准确地将多个表格合并成一个,为用户提供...
点击进入详情页
本回答由TableDI提供
展开全部
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile {
public static void main(String[] args) throws IOException {
String fileContent = readFileContent("");
System.out.println(fileContent);
}
//参数string为你的文件名
private static String readFileContent(String fileName) throws IOException {
File file = new File(fileName);
BufferedReader bf = new BufferedReader(new FileReader(file));
String content = "";
StringBuilder sb = new StringBuilder();
while(content != null){
content = bf.readLine();
if(content == null){
break;
}
sb.append(content.trim());
}
bf.close();
return sb.toString();
}
}
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile {
public static void main(String[] args) throws IOException {
String fileContent = readFileContent("");
System.out.println(fileContent);
}
//参数string为你的文件名
private static String readFileContent(String fileName) throws IOException {
File file = new File(fileName);
BufferedReader bf = new BufferedReader(new FileReader(file));
String content = "";
StringBuilder sb = new StringBuilder();
while(content != null){
content = bf.readLine();
if(content == null){
break;
}
sb.append(content.trim());
}
bf.close();
return sb.toString();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Reader reader = null;
BufferedReader br = null;
try {
reader = new FileReader("txt文件地址");
br = new BufferedReader(reader);
StringBuffer sb = new StringBuffer();
String data = null;
while ((data = br.readLine()) != null) {
sb.appand(data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
BufferedReader br = null;
try {
reader = new FileReader("txt文件地址");
br = new BufferedReader(reader);
StringBuffer sb = new StringBuffer();
String data = null;
while ((data = br.readLine()) != null) {
sb.appand(data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询