1. 程式人生 > >TXT檔案上傳下載

TXT檔案上傳下載

生成UUID

    UUID uuid=UUID.randomUUID();
    System.out.println("uuid======"+uuid);

檔案上傳下載

package com.imt.sys.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.UUID;

import org.junit.Test;
import
org.springframework.test.context.TestExecutionListeners; public class uploadText { private String file = ""; //儲存字串到檔案中 public void saveAsFileWriter(String content,UUID uuid) { file="F:/text/"+uuid+".txt"; FileWriter fwriter = null; try { fwriter = new FileWriter(file); fwriter.write(content); } catch
(IOException ex) { ex.printStackTrace(); } finally { try { fwriter.flush(); fwriter.close(); } catch (IOException ex) { ex.printStackTrace(); } } } //讀取本地文字 public String readFileByLines(UUID uuid) { File file = new
File("F:/text/"+uuid+".txt"); BufferedReader reader = null; String content=""; try { System.out.println("以行為單位讀取檔案內容,一次讀一整行:"); reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // 一次讀入一行,直到讀入null為檔案結束 while ((tempString = reader.readLine()) != null) { //加上p標籤 ,前臺顯示可以換行 tempString="<p>"+tempString+"</p>"; // 顯示行號 System.out.println("line?????????????????????????????????? " + line + ": " + tempString); content=tempString; line++; } }catch(IOException e){ } return content; } }