关于文本内容转字符串的问题 string 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class H20121012 { /** * 功能:Java读取txt文件的内容 * 步骤:1:先获得文件句柄 * 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取 * 3:读取到输入流后,需要读取生成字节流 * 4:一行一行的输出。readline()。 * 备注:需要考虑的是异常情况 * @param filePath */ public static void readTxtFile(String filePath){ try { String encoding="GBK"; File file=new File(filePath); if(file.isFile() && file.exists()){ //判断文件是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file),encoding);//考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while((lineTxt = bufferedReader.readLine()) != null){ System.out.println(lineTxt); } read.close(); }else{ System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } } public static void main(String argv[]){ String filePath = "d:\\20141208.txt";// "res/"; readTxtFile(filePath); } } import java.io.*;public class Test { public static String loadFile(String filename){ FileReader reader = null; char[] buf = new char[50]; StringBuilder sb = new StringBuilder(); int c; try{ reader = new FileReader(new File(filename)); while((c = reader.read(buf))!=-1){ sb.append(buf,0,c); } }catch(IOException e){ }finally{ try{reader.close();}catch(IOException e){} } return sb.toString(); } public static boolean savaFile(String filename,String contents){ FileWriter writer = null; try{ writer = new FileWriter(new File(filename)); writer.write(contents,0,contents.length()); return true; }catch(IOException e){ return false; }finally{try{writer.close();}catch(IOException e){}} } public static void main(String[] args){ System.out.println(loadFile("f1.txt")); savaFile("f2.txt","my conetnts"); }} J2SE面试题总结(英文) 请教一个正则表达式 如何实现这一功能 jar中无法运行,估计是找不到图片 struts 的效率高吗? JDBC 可否支持执行语句块? 小鸟请教,,文件问题。!! com.ms.activeX.ActiveXComponent类哪里找得到啊? 在大连有学JAVA 的朋友吗? 如何让applet读写本地文件? 序列化和反序列化 如何通过外部的代码在JFrame上画图
public class H20121012 {
/**
* 功能:Java读取txt文件的内容
* 步骤:1:先获得文件句柄
* 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取
* 3:读取到输入流后,需要读取生成字节流
* 4:一行一行的输出。readline()。
* 备注:需要考虑的是异常情况
* @param filePath
*/
public static void readTxtFile(String filePath){
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
public static void main(String argv[]){
String filePath = "d:\\20141208.txt";
// "res/";
readTxtFile(filePath);
}
}
public class Test {
public static String loadFile(String filename){
FileReader reader = null;
char[] buf = new char[50];
StringBuilder sb = new StringBuilder();
int c;
try{
reader = new FileReader(new File(filename));
while((c = reader.read(buf))!=-1){
sb.append(buf,0,c);
}
}catch(IOException e){
}finally{
try{reader.close();}catch(IOException e){}
}
return sb.toString();
}
public static boolean savaFile(String filename,String contents){
FileWriter writer = null;
try{
writer = new FileWriter(new File(filename));
writer.write(contents,0,contents.length());
return true;
}catch(IOException e){
return false;
}finally{try{writer.close();}catch(IOException e){}}
}
public static void main(String[] args){
System.out.println(loadFile("f1.txt"));
savaFile("f2.txt","my conetnts");
}
}