如何将一篇txt文章内的文字都放入到一个string中?请会的人帮忙解答 你为什么要读入String啊!String对象的容量是有限的!如果读入大的数据,会造成溢出!最好的办法就是利用StringBuffer来读取!可以动态的根据读入的文件的大小申请空间! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我主要是为了好处理文字内容,放在String还是StringBuffer中无所谓。我也知道要用到输入流的知识,我就是想知道如何将其中文字信息包装到一个String或是StringBuffer中,具体怎么做呀,里面字符不能有丢失,速度效率要高。请帮忙解答呀。会的帮帮忙呀 BufferedReader reader = new BufferedReader(new FileReader("x.txt"));StringBuffer buffer;String line;while ((line = reader.readLine()) != null) { buffer.append(line); buffer.append("\n");}reader.close();String whichYouWant = buffer.toString(); FileInputStream inFile = null; try { inFile= new FileInputStream (file); }catch(FileNotFoundException ex){ ex.printStackTrace(); } FileChannel inChannel = inFile.getChannel(); ByteBuffer buf =null; try{ buf=ByteBuffer.allocate((int)inChannel.size()); } catch(IOException ex2){ ex2.printStackTrace(); } String str=null; try { while(inChannel.read(buf)!=-1) { str=new String(buf.array()); buf.clear(); } inFile.close(); } catch(IOException ex1) { ex1.printStackTrace(); } 怎么依次打开一个文件夹里面所有的文件? Java中生成Unixtime时间 做一个释放内存的软件 在生成子类对象的时候是否也生成父类的对象;加载方面的一些问题 java技术交流站 群号:2303735 看“四天学会Java”帖子有感:论Java版的叫骂 求救....rh9下装jdk1.4出现问题。。。。。 请问如何连数据库? 大家帮忙看段代码(小弟初学) 有关java与web服务器的通讯的问题 从数据库中生成报表,在web端修改后又存回去 关于逻辑表达式的问题
StringBuffer buffer;
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
buffer.append("\n");
}
reader.close();
String whichYouWant = buffer.toString();
try
{
inFile= new FileInputStream (file);
}catch(FileNotFoundException ex){
ex.printStackTrace();
}
FileChannel inChannel = inFile.getChannel();
ByteBuffer buf =null;
try{
buf=ByteBuffer.allocate((int)inChannel.size());
}
catch(IOException ex2){
ex2.printStackTrace();
}
String str=null;
try
{
while(inChannel.read(buf)!=-1)
{
str=new String(buf.array());
buf.clear();
}
inFile.close();
}
catch(IOException ex1)
{
ex1.printStackTrace();
}