//一次只读一个字节
try{
File file = new File(*文件的路径*);
Memcached mcc = new MemcachedClient("127.0.0.1",11211);
mcc.add("file",900,file);
System.out.println("file对象已经被缓存");
Flie f =(File) mcc.get("file");
InputStream in= new FileInputStream(f);
int tempByte;
while((tempByte = in.read())!= -1){
System.out.println(tempByte);
System.out.write(tempByte);
}
}
catch(FileNotFoundException e){
System.out.println(e.getMessage());
}
catch(Exception e){
System.out.println(e.getMessage());
}
//**memcached:基于缓存进行数据存取--介质:内存
//**file:基于磁盘文件进行数据存取--介质:磁盘
//以字节为单位读取文件,一次只读多个字节
try{
File file = new File(*文件的路径*);
InputStream in = new FileInputStream(file);
byte[] bytes = new byte[100];
int temp;
while((temp=(in.read(bytes)))!=-1){
System.out.write(bytes,0,temp);
}
}
catch(FileNotFoundException e){
System.out.println(e.getMessage());
}
catch(Exception e){
System.out.println(e.getMessage());
}
//以字符为单位读取文件,一次读取一个字符
try{
File file = new File(*文件的路径*);
Reader reader = new InputStreamReader( new FileInputStream(file));
int temp ;
while((temp=reader.read(temp))!=-1){
System.out.println(temp);
}
}
catch(FileNotFoundException e){
System.out.println(e.getMessage());
}
catch(Exception e){
System.out.println(e.getMessage());
}
//以字符为单位读取文件,一次读取多个字符
try{
File file = new File(*文件的路径*);
char[] tempChar = new char[30];
int chared = 0;
Reader reader = new InputStreamReader( new FileInputStream(file));
while((chared=reader.read(tempChar))!=-1){
System.out.println(tempChar);
}
}
catch(FileNotFoundException e){
System.out.println(e.getMessage());
}
catch(Exception e){
System.out.println(e.getMessage());
}
//以行为单位读取文件(一次读取一行内容)
try{
File file = new File(*文件的路径*);
BufferedReader br = new BufferedReader(new FileReader(file));
String temp = null;
while ((temp=br.readLine())!= null){
System.out.println(temp);
}
}
catch(FileNotFoundException e){
System.out.println(e.getMessage());
}
catch(Exception e){
System.out.println(e.getMessage());
}
//以字节为单位写入文件
try{
File file = new File(*文件的路径*);
OutputStream out = new FileOutputStream(file);
String content = "周一java--周二数据库原理--周三网页制作";
byte[] b = content.getBytes();
out.write(b);
out.close();
System.out.println("文件写入成功");
}
catch(Exception e){
System.out.println(e.getMessage());
}
//以字符为单位写入文件
try{
File file = new File(*文件的路径*);
Writer writer = new OutputStreamWriter(new FileOutputStream(file));
String content = "周一java--周二数据库原理--周三网页制作";
writer.write(content);
writer.close();
System.out.println("文件写入成功");
}
catch(Exception e){
System.out.println(e.getMessage());
}
//以行为单位写入文件
try{
File file = new File(*文件的路径*);
PrintWriter pw = new PrintWriter(new FileOutputStream(file));
String content = "周一java--周二数据库原理--周三网页制作";
pw.print(content);
pw.close();
System.out.println("文件写入成功");
}
catch(Exception e){
System.out.println(e.getMessage());
}