java輸出文件,java輸出文件夾下所有文件名

本文目錄一覽:

如何用java輸出txt文件

輸入無需使用位元組流,直接字符流讀取即可。

private void input(String fileName) throws IOException {

    try(BufferedReader reader = new BufferedReader(new FileReader(fileName))) {

        String line;

        while((line=reader.readLine()) != null) {

            System.out.println(line);

        }   

    }

}

同樣輸出,只要把Input換成Output;

private void output(String fileName, String content) throws IOException{

    try(BufferedWriter  writer = new BufferedWriter(new FileWriter(fileName))) {

        writer.write(content);

        writer.flush();

    }

}

java怎麼輸出pdf格式的文件

java導出pdf需要用到iText庫,iText是著名的開放源碼的站點sourceforge一個項目,是用於生成PDF文檔的一個java類庫。通過iText不僅可以生成PDF或rtf

的文檔,而且可以將XML、Html文件轉化為PDF文件。

iText的安裝非常方便,下載iText.jar文件後,只需要在系統的CLASSPATH中加入iText.jar的路徑,在程序中就可以使用

iText類庫了。

代碼如下:

public class createPdf {

//自己做的一個簡單例子,中間有圖片之類的

//先建立Document對象:相對應的 這個版本的jar引入的是com.lowagie.text.Document

Document document = new Document(PageSize.A4, 36.0F, 36.0F, 36.0F, 36.0F);

public void getPDFdemo() throws DocumentException, IOException{

//這個導出用的是 iTextAsian.jar 和iText-2.1.3.jar 屬於比較老的方法。 具體下在地址見:

//首先

//字體的定義:這裡用的是自帶的jar裏面的字體

BaseFont bfChinese = BaseFont.createFont(“STSong-Light”, “UniGB-UCS2-H”, false);

// 當然你也可以用你電腦裏面帶的字體庫

//BaseFont bfChinese = BaseFont.createFont(“C:/WINDOWS/Fonts/SIMSUN.TTC,1”,BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

//定義字體 注意在最新的包裏面 顏色是封裝的

Font fontChinese8 = new Font(bfChinese, 10.0F, 0, new Color(59, 54, 54));

//生成pdf的第一個步驟:

//保存本地指定路徑

saveLocal();

document.open();

ByteArrayOutputStream ba = new ByteArrayOutputStream();

// PdfWriter writer = PdfWriter.getInstance(document, ba);

document.open();

//獲取此編譯的文件路徑

String path = this.getClass().getClassLoader().getResource(“”).getPath();

//獲取根路徑

String filePath = path.substring(1, path.length()-15);

//獲取圖片路徑 找到你需要往pdf上生成的圖片

//這裡根據自己的獲取的路徑寫 只要找到圖片位置就可以

String picPath = filePath +”\\WebContent” +”\\images\\”;

//往PDF中添加段落

Paragraph pHeader = new Paragraph();

pHeader.add(new Paragraph(” 你要生成文字寫這裡”, new Font(bfChinese, 8.0F, 1)));

//pHeader.add(new Paragraph(“文字”, 字體 可以自己寫 也可以用fontChinese8 之前定義好的 );

document.add(pHeader);//在文檔中加入你寫的內容

//獲取圖片

Image img2 = Image.getInstance(picPath +”ccf-stamp-new.png”);

//定義圖片在文檔中顯示的絕對位置

img2.scaleAbsolute(137.0F, 140.0F);

img2.setAbsolutePosition(330.0F, 37.0F);

//將圖片添加到文檔中

document.add(img2);

//關閉文檔

document.close();

/*//設置文檔保存的文件名

response.setHeader(“Content-

disposition”, “attachment;filename=\””+ new String((“CCF會員資格確認

函.pdf”).getBytes(“GBK”),”ISO-8859-1″) + “\””);

//設置類型

response.setContentType(“application/pdf”);

response.setContentLength(ba.size());

ServletOutputStream out = response.getOutputStream();

ba.writeTo(out);

out.flush();*/

}

public static void main(String[]args) throws DocumentException, IOException{

createPdf pdf= new createPdf();

pdf.getPDFdemo();

}

//指定一個文件進行保存 這裡吧文件保存到D盤的text.pdf

public void saveLocal() throws IOException, DocumentException{

//直接生成PDF 制定生成到D盤test.pdf

File file = new File(“D:\\text2.pdf”);

file.createNewFile();

PdfWriter.getInstance(document, new FileOutputStream(file));

}

}

java怎樣輸出excel文件

//java生成簡單的Excel文件

package beans.excel;

import java.io.IOException;

import java.io.OutputStream;

import jxl.Workbook;

import jxl.write.Label;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

import jxl.write.WriteException;

 

public class SimpleExcelWrite {

    public void createExcel(OutputStream os) throws WriteException,IOException{

        //創建工作薄

        WritableWorkbook workbook = Workbook.createWorkbook(os);

        //創建新的一頁

        WritableSheet sheet = workbook.createSheet(“First Sheet”,0);

        //創建要顯示的內容,創建一個單元格,第一個參數為列坐標,第二個參數為行坐標,第三個參數為內容

        Label xuexiao = new Label(0,0,”學校”);

        sheet.addCell(xuexiao);

        Label zhuanye = new Label(1,0,”專業”);

        sheet.addCell(zhuanye);

        Label jingzhengli = new Label(2,0,”專業競爭力”);

        sheet.addCell(jingzhengli);

        

        Label qinghua = new Label(0,1,”清華大學”);

        sheet.addCell(qinghua);

        Label jisuanji = new Label(1,1,”計算機專業”);

        sheet.addCell(jisuanji);

        Label gao = new Label(2,1,”高”);

        sheet.addCell(gao);

        

        Label beida = new Label(0,2,”北京大學”);

        sheet.addCell(beida);

        Label falv = new Label(1,2,”法律專業”);

        sheet.addCell(falv);

        Label zhong = new Label(2,2,”中”);

        sheet.addCell(zhong);

        

        Label ligong = new Label(0,3,”北京理工大學”);

        sheet.addCell(ligong);

        Label hangkong = new Label(1,3,”航空專業”);

        sheet.addCell(hangkong);

        Label di = new Label(2,3,”低”);

        sheet.addCell(di);

        

        //把創建的內容寫入到輸出流中,並關閉輸出流

        workbook.write();

        workbook.close();

        os.close();

    }

    

}

Java中如何實現文件的輸入和輸出?

程序如下:

span style=”color:#990000;”

/spanFile file1 = new File(“/home/a123/a”);

if (file1.exists()) {

System.out.println(“存在文件夾a”);

} else {

file1.mkdir(); // 文件夾的創建 創建文件夾/home/a123/a

}

File file2 = new File(“/home/a123/a/test”);

if (file2.exists()) {

System.out.println(“存在文件夾或者文件test”);

} else {

try {

file2.createNewFile(); // 文件的創建,注意與文件夾創建的區別

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

* 最簡單的文件讀寫方法是使用類FileWriter

* (它的父類依次是java.io.OutputStreamWriter——java.io.Writer——java.lang.Object );

*/

// 下面是向文件file2裏面寫數據

try {

FileWriter fileWriter = new FileWriter(file2);

String s = new String(“This is a test! \n” + “aaaa”);

fileWriter.write(s);

fileWriter.close(); // 關閉數據流

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

/*

* 這樣寫數據的話,是完全更新文件test裏面的內容,即把以前的東西全部刪除,重新輸入。

* 如果不想刪除以前的數據,而是把新增加的內容增添在文件末尾處。只需要在創建FileWriter對象時候,使用另外一個構造函數即可:

* FileWriter fileWriter=new FileWriter(file2,true);

*/

// 下面是從文件file2讀東西

try {

FileReader fileReader = new FileReader(file2);

String s = null;

char ch;

try {

char[] c = new char[100];

fileReader.read(c,0,2); // 具體想得到文件裏面的什麼值(單個char?int?還是String?),

System.out.println(c);

fileReader.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

/**

* 具體想得到文件裏面的什麼值(單個char?int?還是String?),需要知道不通read的不同用法:

* 1. int read() 讀取單個字符。

* 2. int read(char[] cbuf) 將字符讀入數組。 可以再將字符型數組轉化位字符串

* 3. int read(char[] cbuf,int off,int len) 將字符讀入數組的某一部分。

* 這三個方法都返回一個int值,作用是:讀取的字符數,如果已到達流的末尾,則返回 -1.

*/

}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/196060.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-03 09:53
下一篇 2024-12-03 09:53

相關推薦

發表回復

登錄後才能評論