1.Servlet分为哪几类?
答:
标准Servlet,
过滤Servlet(过滤器):是驻留在服务端的Web组件
监听Servlet(监听器)
2.写一个程序将小写金额转换成大写金额?
例如:1000.25元
一千零两块*
复习Io流,流(Api),RandomAccessFile
3.File
答:文件和目录路径名的抽象表示形式。
例:
创建文件
public static void main(String[] args) {
// TODO Auto-generated method stub
File>try {
System.out.println(file.createNewFile());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
指定创建目录
public static void main(String[] args) {
// TODO Auto-generated method stub
File>System.out.println(d.mkdir());
}
查找文件和后缀名
public static void main(String[] args) {
// TODO Auto-generated method stub
File>listFiles(file);
}
private static void listFiles(File file) {
// TODO Auto-generated method stub
File[]>for (File file2 : files) {
if(file2.isFile()){
System.out.println(file2);
}else{
listFiles(file2);
}
}
}
找某个文件含有.java的文件?
public static void main(String[] args) {
// TODO Auto-generated method stub
File>File []>
@Override
public boolean accept(File dir, String name) {//Override FilenameFilter 方法
// TODO Auto-generated method stub
//System.out.println(dir+":"+name);
return name.endsWith(".java"); //返回含有。java的文件
}
});
for (File file2 : files) {//遍历打印
System.out.println(file2);
}
}
将某一个A.java文件拷贝到另一个文件夹下?
public static void main(String[] args) {
// TODO Auto-generated method stub
File>File>copyFile(file,fileDest);
}
private static void copyFile(File file, File fileDest) {
// TODO Auto-generated method stub
FileInputStream>FileOutputStream>try {
input = new FileInputStream(file);//放入要Copy的文件
output = new FileOutputStream(fileDest);//
int>try {
/*第一种方法一个一个的读*/
//while((hasRead=input.read())!=-1){
//output.write(hasRead);
//}
/*第二种方法字节流*/
byte []>while((hasRead=input.read())>0){
output.write(buffs, 0, hasRead);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(input != null){
input.close();
}
if(output != null){
output.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*第三种方法字符流*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File>File>// copyFile(file,fileDest);
copyFileByCharStream(file, fileDest);
}
private static void copyFileByCharStream(File file, File fileDest) {
// TODO Auto-generated method stub
FileReader>FileWriter>try {
reader = new FileReader(file);
writer = new FileWriter(fileDest);
int>char[]>while ((hasRead = reader.read(cbuf)) > 0) {
writer.write(cbuf, 0, hasRead);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (null != reader) {
reader.close();
}
if(null != writer){
writer.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
{高级流BufferedReader,BufferedWriter}
public static void main(String[] args) {
// TODO Auto-generated method stub
File>File>// copyFile(file,fileDest);
// copyFileByCharStream(file, fileDest);
copyFileByBufferStream(file, fileDest);
}
/* 一行一行的读取 */
private static void copyFileByBufferStream(File file, File fileDest) {
// TODO Auto-generated method stub
BufferedReader>BufferedWriter>try {
reader = new BufferedReader(new FileReader(file));
writer = new BufferedWriter(new FileWriter(fileDest));
String>while ((str =>writer.write(str + "\r\n");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (null != reader) {
reader.close();
}
if (null != writer) {
writer.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
将一个D:\\java文件下的所有后缀名为java的文件拷贝到D:\\jad目录下,并将他改名为后缀为.jad?
public static void main(String[] args) {
// TODO Auto-generated method stub
File>File[]>
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return name.endsWith(".java");
}
});
File>if(!fd.exists()){
fd.mkdir();
}
for (File file2 : files) {
copyFile(file2,new File(fd+"/"+file2.getName().replaceAll("\\.java$", ".jad")));
}
}
private static void copyFile(File file2, File dest) {
// TODO Auto-generated method stub
BufferedReader>BufferedWriter>try {
reader = new BufferedReader(new FileReader(file2));
writer = new BufferedWriter(new FileWriter(dest));
String>while ((str =>writer.write(str + "\r\n");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (null != reader) {
reader.close();
}
if (null != writer) {
writer.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}