请问先用输入流读取一个.txt文件,怎样用int read(char [])方法将.txt内容转换为字符串数组
2个回答
展开全部
import java.io.File;
import java.io.FileInputStream;
public class FileTest {
public static void main(String[] args) {
FileInputStream fis = null;
try {
File file = new File("D://test.txt");
fis = new FileInputStream(file);
byte[] b = new byte[(int)file.length()];
while (fis.read(b) != -1) {
}
System.out.println(new String(b));
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.FileInputStream;
public class FileTest {
public static void main(String[] args) {
FileInputStream fis = null;
try {
File file = new File("D://test.txt");
fis = new FileInputStream(file);
byte[] b = new byte[(int)file.length()];
while (fis.read(b) != -1) {
}
System.out.println(new String(b));
} catch (Exception e) {
e.printStackTrace();
}
}
}
展开全部
用哪个 bufferedReader 读。
FileReader fr =new FileReader("...txt");
BufferedReader br=new BufferedReader(fr);
StringBuffer sb = new StringBuffer();
String str = null;
while ((str = br.readLine()) != null)
sb.append(str);
FileReader fr =new FileReader("...txt");
BufferedReader br=new BufferedReader(fr);
StringBuffer sb = new StringBuffer();
String str = null;
while ((str = br.readLine()) != null)
sb.append(str);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询