java文件读取和文件创建

怼烎@ 2024-05-23 14:13 118阅读 0赞

java文件读取和文件创建

文章目录

  • java文件读取和文件创建
  • 前言
  • 一、任意文件创建
  • 二、任意文件读取

前言

java文件读取和文件创建,在jsp中的应用
pom文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>org.example</groupId>
  7. <artifactId>webvul</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <build>
  10. <plugins>
  11. <plugin>
  12. <groupId>org.apache.maven.plugins</groupId>
  13. <artifactId>maven-compiler-plugin</artifactId>
  14. <configuration>
  15. <source>8</source>
  16. <target>8</target>
  17. </configuration>
  18. </plugin>
  19. </plugins>
  20. </build>
  21. <dependencies>
  22. <dependency>
  23. <groupId>javax.servlet</groupId>
  24. <artifactId>javax.servlet-api</artifactId>
  25. <version>4.0.1</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>javax.servlet</groupId>
  29. <artifactId>jsp-api</artifactId>
  30. <version>2.0</version>
  31. <scope>provided</scope>
  32. </dependency>
  33. </dependencies>
  34. </project>

一、任意文件创建

  1. <%@ page import="java.io.FileOutputStream" %>
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <html>
  4. <head>
  5. <title>fileCreate</title>
  6. </head>
  7. <body>
  8. <%
  9. FileOutputStream fos = new FileOutputStream("C:\\Users\\fly\\IdeaProjects\\webvul\\web\\"+request.getParameter("name"));
  10. fos.write(request.getParameter("c").getBytes());
  11. fos.flush();
  12. fos.close();
  13. %>
  14. </body>
  15. </html>

成功创建test.txt文件
在这里插入图片描述
执行成功
在这里插入图片描述

二、任意文件读取

  1. <%@ page import="java.io.FileInputStream" %>
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <html>
  4. <head>
  5. <title>fileRead</title>
  6. </head>
  7. <body>
  8. <%
  9. FileInputStream fileInputStream = new FileInputStream("C:\\Users\\fly\\IdeaProjects\\webvul\\web\\"+request.getParameter("file"));
  10. int tmp;
  11. while ((tmp=fileInputStream.read())!=-1){
  12. out.write(tmp);
  13. }
  14. %>
  15. </body>
  16. </html>

在这里插入图片描述
在这里插入图片描述


发表评论

表情:
评论列表 (有 0 条评论,118人围观)

还没有评论,来说两句吧...

相关阅读