本地文件与资源文件(Java EE)

一、本地文件

文件路径

在Web项目里,文件的路径可以指定绝对路径,也可以使用相对路径

示例:实现一个功能,在页面输入标题和内容,后将数据保存到文件中

-保存到data/目录中

-每次根据标题创建一个文件

前端代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>信息的保存</title>
	</head>
	
	<script type="text/javascript" src="JS/jquery-3.3.1.min.js"></script>
	<script type="text/javascript" src="JS/afquery.js"></script>
	
	<style>
		.main{
			width: 600px;
			margin: 10px auto;
		}
		.main .title{
			width: 100%;
			box-sizing: border-box;
			margin: 4px 0px;
		}
		.main .content{
			width: 100%;
			height: 150px;
			box-sizing: border-box;
			margin: 4px 0px;
		}
	</style>
	<body>
		<div class="main">
			<div>
				<input type="text"  class="title"  placeholder="标题"/>
			</div>
			<div>
				<textarea class="content" placeholder="内容"></textarea>
			</div>
			<div>
				<button οnclick="save()">保存</button>
			</div>
		</div>
	</body>
	<script>
		window.save = function()
		{
			var req = {};
			req.title = $(".main .title").val().trim();
			req.content = $(".content").val().trim();
			Af.rest("SaveContent", req, function(data){
				alert("保存成功!");
				$(".main .title").val("");  //置空
				$(".main .content").val("");
			})
		}
	</script>
</html>

后端代码:

package my;

import java.io.File;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import af.AfSimpleREST;
import af.AfTextFile;
@WebServlet("/SaveContent")
public class Sava extends AfSimpleREST
{

	@Override
	protected Object execute(HttpServletRequest request, 
								HttpServletResponse response, 
									JSONObject jreq) throws Exception
	{
		String title = jreq.getString("title");
		String content = jreq.getString("content");
		
		//获取项目实际路径
		//-获取对象-当前运行文件在服务器上的绝对路径.
		String webRootPath = request.getServletContext().getRealPath("/");
		System.out.println(webRootPath);
		File webroot = new File(webRootPath);
		
		File dir = new File(webroot, "data/");
		dir.mkdirs();
		
		File f = new File(dir, title + ".txt");
		AfTextFile.write(f, content, "utf-8");
		return null;
	}
}
package af;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class AfTextFile
{
	//读取
	public static String read (File f, String charset) throws Exception
	{
        FileInputStream fstream = new FileInputStream(f);
        try{
            int fileSize = (int)f.length();
            if(fileSize > 1024*512)
                throw new Exception("File too large to read! size=" + fileSize);

            byte[] buffer = new byte[ fileSize ];
            fstream.read(buffer);
            return new String(buffer, charset);
        }finally
        {
            try{ fstream.close();}catch (Exception e){}
        }
	}
	//写入
	public static void write(File f, String text, String charset) throws Exception
	{
		FileOutputStream fstream = new FileOutputStream(f);
		
        try{
            fstream.write( text.getBytes( charset ));
        }finally
        {
            fstream.close();
        }
	}
}

二、资源文件

在java项目中,资源文件是指存放在Classpath里的文件

资源文件的路径和类的路径类似

例如  /config.xml          /my/example/logo.png

在项目中使用资源文件

可在程序里访问资源文件

InputStream inputStream = getClass().getResourceAsStream("/Course.xml");

注:

-资源文件是只读的

-资源文件路径和类路径写法相同

-程序实际访问的不是src下的文件,而是classes下的文件

发布了246 篇原创文章 · 获赞 22 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/gjs935219/article/details/102618590