5.21
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class FileToUnicode {
public static void main(String args[]){
try{
FileInputStream fis = new FileInputStream("file1.txt");
InputStreamReader dis = new InputStreamReader(fis);
BufferedReader reader = new BufferedReader(dis);
String s;
while ((s = reader.readLine())!= null){
System.out.println("read:"+s);
}
dis.close();
}catch(IOException e){
System.out.println(e);
}
}//main()
}//class
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class PipedStreamDemo {
public static void main(String args[])throws IOException
{
//建立管道连接
PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pos);
//数据赋值
byte datamover = 0;
System.out.println("start work...");
try{
System.out.println("transfer "+ datamover +" to pos.\n");
//写入数据
pos.write(datamover);
//读取数据
System.out.println("pis get "+(byte)pis.read());
}finally{
pis.close();
pos.close();
}
}
}
import java.io.File;
public class UseFile {
public static void main(String args[]){
File f = new File("file1.txt");
System.out.println("the file is exists? "+ f.exists());
System.out.println("the file is write? " + f.canWrite());
System.out.println("the file is read ? "+ f.canRead());
System.out.println("the file is a file " +f.isFile());
System.out.println("the file's path is ? "+ f.getPath());
System.out.println("the file's absolute path is "+ f.getAbsolutePath());
System.out.println("the file's parent path is ?" + f.getParent());
File newfile = new File("newfile.txt");
f.renameTo(newfile);
System.out.println(newfile.getName());
System.out.println("if file is exists?" + f.exists());
f.delete();
newfile.delete();
System.out.println("if file is exists?" + f.exists());
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CharInput {
public static void main(String args[]) throws IOException {
String s;
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(ir);
while ((s = in.readLine())!= null){
System.out.println("Read:"+ s );
}
}
}
import java.io.*;
public class FileInputStreamTest {
public static void main(String[] args) {
try{
FileInputStream in = new FileInputStream("myfile.dat");
while(in.available()>0){
System.out.println(in.read()+ " ");
}
in.close();
}catch(FileNotFoundException e){
System.out.println("Error:Cannot open file for reading ");
}catch(EOFException e){
System.out.println("Error:EOF");
}catch(IOException e ){
System.out.println("Error:Cannot read from file ");
}
}
}
import java.io.*;
public class FileOutputStreamTest{
public static void main (String args[]){
try{
FileOutputStream out = new FileOutputStream("myFile.dat");
out.write('H');
out.close();
}catch(FileNotFoundException e) {
System.out.println("Error:Cannot open file for writing.");
}catch(IOException e){
System.out.println("Error:Cannot write to file.");
}
}
}
package com.xxxx.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/s01")
public class Servlet01 extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 通过session获取
ServletContext servletContext1 = request.getServletContext();
// 通过request获取
ServletContext servletContext2 = request.getSession().getServletContext();
// 通过ServletConfig获取
ServletContext servletContext3 = getServletConfig().getServletContext();
// 直接获取
ServletContext servletContext4 = getServletContext();
// 获取根路径
String realPath = request.getServletContext().getRealPath("/");
System.out.println("真实路径:"+realPath);
}
}
package com.xxxx.wenjian;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
@WebServlet("/uploadServlet")
@MultipartConfig
public class UploadServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("文件上传");
// 设置请求的编码格式
request.setCharacterEncoding("UtF-8");
String uname = request.getParameter("uname");
System.out.println("uname"+ uname);
// 获取Part对象(Servlet将Post请求封装成part对象
Part part = request.getPart("myfile");
// 通过part对象得到上传的文件名
String fileName = part.getSubmittedFileName();
System.out.println("上传文件名:"+ fileName);
// 得到文件的存放路径
String filePath = request.getServletContext().getRealPath("/");
System.out.println("文件存放的路径:"+ filePath);
// 上传文件到指定目录
part.write(filePath+"/"+ fileName);
}
}
package com.xxxx.wenjian;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@WebServlet("/downloadServlet")
public class DownloadServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("下载文件");
// 编码一致
response.setContentType("text/html; charset=UTF-8");
request.setCharacterEncoding("UTF-8");
// 得到要下载的文件名
String fileName = request.getParameter("fileName");
// 非空判断
if(fileName == null ||"".equals(fileName.trim())){
response.getWriter().write("请输入下载的文件名");
response.getWriter().close();
return;
}
// 得到存放路径
String path = request.getServletContext().getRealPath("/download/");
// 通过路径得到file对象
File file = new File(path + fileName);
// 判断文件是否存在
if(file.exists() && file.isFile()){
// 设置响应类型
response.setContentType("text/html; charset=UTF-8");
// 设置响应头
response.setHeader("Content-Disposition","attchment;fileName"+ fileName);