示例:Servlet读取文件内容并在页面打印输出

 1 package com.mhb;
 2
 3 import java.io.BufferedReader;
 4 import java.io.File;
 5 import java.io.FileReader;
 6 import java.io.IOException;
 7 import java.io.PrintWriter;
 8
 9 import javax.servlet.ServletException;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13
14 public class FileRead extends HttpServlet {
15
16 public void init() throws ServletException {
17 }
18
19 public void doGet(HttpServletRequest request, HttpServletResponse response)
20 throws ServletException, IOException {
21 response.setContentType("text/html");     //设置响应内容格式
22 response.setCharacterEncoding("gb2312");    //设置响应内容编码
23 PrintWriter out = response.getWriter();     //获得out对象
24 String fileName = "content.txt";     //指定文件名称
25 String realPath = request.getRealPath(fileName);
26
27 File file = new File(realPath);
28
29 if(file.exists()){
30 FileReader reader = new FileReader(file);    //获得输入流
31 BufferedReader bufferReader = new BufferedReader(reader); //使用缓冲流
32 String line = null;     //每行数据
33 while ((line = bufferReader.readLine()) != null){    //循环读取
34 out.print(line +"<br />");     //输出文件内容
35 }
36 }else{
37 out.print("文件不存在!");
38 }
39
40 }
41
42 public void doPost(HttpServletRequest request, HttpServletResponse response)
43 throws ServletException, IOException {
44 }
45
46 public void destroy() {
47 super.destroy();
48 }
49 }

文本文件:content.txt内容

Java编程
C++编程
C#编程

浏览器显示:

时间: 2024-08-30 07:23:14

示例:Servlet读取文件内容并在页面打印输出的相关文章

php读取文件内容的多种方法示例代码

分享下php中读取文件内容的几种方法,各有千秋,与大家共勉. 示例代码1: 用file_get_contents 以get方式获取内容<?php$url='http://www.jbxue.com/';$html=file_get_contents($url);//print_r($http_response_header);ec($html);printhr();printarr($http_response_header);printhr();?> 示例代码2: 用fopen打开url,

PHP如何正确读取文件内容?解析

PHP 读取文件的多种方法,一起来看看吧. 处理诸如 PHP 之类的现代编程语言的乐趣之一就是有大量的选项可用.PHP 可以轻松地赢得 Perl 的座右铭“There's more than one way to do it”(并非只有一种方法可做这件事),尤其是在文件处理上.但是在这么多可用的选项中,哪一种是完成作业的最佳工具?当然,实际答案取决于解析文件的目标,因此值得花时间探究所有选项. 回页首 传统的 fopen 方法 fopen 方法可能是以前的 C 和 C++ 程序员最熟悉的,因为如

asp.net 上传XML,txt 直接读取文件内容

if (GetUploadFileContent.PostedFile.InputStream.Length < 1) { Msg.Text = "请选择文件";return; } string FileName = GetUploadFileContent.FileName;//上传文件文件名 string FilePath = GetUploadFileContent.PostedFile.FileName;//上传文件完整路径+文件名string fileExtName =

PHP读取文件内容的五种方式

php读取文件内容的五种方式 分享下php读取文件内容的五种方法:好吧,写完后发现文件全部没有关闭.实际应用当中,请注意关闭 fclose($fp);-- php读取文件内容: -----第一种方法-----fread()-------- ? 1 2 3 4 5 6 7 8 <?php $file_path = "test.txt"; if(file_exists($file_path)){ $fp = fopen($file_path,"r"); $str

使用ifstream和getline读取文件内容[c++]

转载:http://www.cnblogs.com/JCSU/articles/1190685.html 假设有一个叫 data.txt 的文件, 它包含以下内容: Fry: One Jillion dollars. [Everyone gasps.] Auctioneer: Sir, that's not a number. 数据读取, 测试 . 以下就是基于 data.txt 的数据读取操作: #include <iostream> #include <fstream> #in

7 RandomAccessFile读取文件内容保存--简单例子(需要验证)

1 import org.slf4j.Logger; 2 import org.slf4j.LoggerFactory; 3 4 import java.io.*; 5 6 /** 7 * 读取动态产生的文件内容 8 */ 9 public class RandomAccessRead { 10 public static Logger logger= LoggerFactory.getLogger(RandomAccessRead.class); 11 12 //文件默认读取位置为从开始读取

php读取文件内容的三种方式(转)

分享下php读取文件内容的三种方法. php读取文件内容: //**************第一种读取方式***************************** header("content-type:text/html;charset=utf-8"); //文件路径 $file_path="text.txt"; //判断是否有这个文件 if(file_exists($file_path)){ if($fp=fopen($file_path,"a+&

Python逐行读取文件内容

Python逐行读取文件内容thefile= open("foo.txt") line = thefile.readline() while line: print line, line = thefile.readline() thefile.close() Windows下文件路径的写法:E:/codes/tions.txt 写文件:thefile= open("foo.txt", "rw+")for item in thelist: the

nodejs常见的读取文件内容的方法

nodejs常见的读取文件内容的方法 by 伍雪颖 var fs = require('fs'); var rs = fs.createReadStream('test.md'); var data = ''; rs.on("data",function(chunk) { data += chunk; }); rs.on("end",function() { console.log(data); });