public String getFileContent(String filePath) throws IOException { // String filePath = "E:\\a.txt"; System.out.println("filePath=====>" + filePath); StringBuilder sb = new StringBuilder(); BufferedReader br = null; if (filePath != null) { InputStreamReader isr = null; try { isr = new InputStreamReader(new FileInputStream( new File(filePath)), "UTF-8"); br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { sb.append(line).append("\n"); } } finally { br.close(); isr.close(); } } return sb.toString(); }
时间: 2024-02-15 14:27:57 浏览: 32
这是一个Java代码,它读取指定文件路径下的文本文件,并将文件内容以字符串形式返回。该方法使用了Java IO中的FileInputStream、InputStreamReader和BufferedReader等类,以及try-finally语句块确保资源被正确关闭。请注意,该方法假定文件编码为UTF-8。
相关问题
@Controller public class ImageController { @RequestMapping("/uploadImage") @ResponseBody public String uploadImage(HttpServletRequest request) throws IOException { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile file = multipartRequest.getFile("file"); //保存文件到本地磁盘 String fileName = file.getOriginalFilename(); String suffix = fileName.substring(fileName.lastIndexOf(".") + 1); // String filePath = "F:/upload/"; String filePath = "D:/ideaprojects/SSM/BookShopSystem/src/main/webapp/static/img/"; String newFileName = UUID.randomUUID().toString() + "." + suffix; File dest = new File(filePath + newFileName); if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } file.transferTo(dest); //返回文件访问路径 String url = "http://localhost:8080/BookShopSystem_war/static/img/" + newFileName; System.out.println("url:"+url); return "{\"code\":0,\"msg\":\"\",\"data\":{\"src\":\"" + url + "\"}}"; } }
这段代码是一个 Java Spring MVC 的控制器类,用于处理图片上传请求。其中,@Controller 注解表示该类是一个控制器,@RequestMapping("/uploadImage") 注解指定了处理上传图片请求的 URL 地址。@ResponseBody 注解表示该方法返回的是 JSON 格式的数据。
在方法内部,首先通过 HttpServletRequest 获取上传的文件。然后,获取文件名和后缀,并使用 UUID 生成一个新的文件名。接着,将文件保存到本地磁盘指定的路径下,并返回文件访问路径(即 URL)。最后,返回一个 JSON 格式的字符串,其中包含上传成功的信息和文件访问路径。
package srsc; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class Srsc { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub String filePath = "D:\\test.txt"; BufferedWriter bw = new BufferedWriter(new FileWriter(filePath)); Scanner scanner = new Scanner(System.in); String name = scanner.next(); String gender = scanner.next(); int age = scanner.nextInt(); Student stu = new Student(name, gender, age); bw.write(stu.toString()); bw.newLine(); BufferedReader br = new BufferedReader(new FileReader(filePath)); String rline; while((rline = br.readLine()) != null) { System.out.println(rline); } bw.close(); br.close(); scanner.close(); } }为什么没有输出到控制台
这是因为在程序中没有使用 `System.out.println()` 或 `System.out.print()` 方法将文本输出到控制台。在程序中,您已经将学生信息写入了文件,并在文件中读取了该信息,但是您需要使用 `System.out.println()` 或 `System.out.print()` 方法将其输出到控制台。
您可以在 `while` 循环中添加以下代码:
```
while((rline = br.readLine()) != null) {
System.out.println(rline); // 将每一行的文本输出到控制台
}
```
这样就可以将文件中的信息输出到控制台了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)