大家好,今天小編來為大家解答fileoutputstream這個問題,bufferedreader讀取大文件很多人還不知道,現在讓我們一起來看看吧!
apk如何能夠讀寫手機data目錄下內容
代碼:
publicvoidbackupApp(StringpackageName)throwsIOException
{
Filein=newFile("/data/app/"+packageName+".apk");
Fileout=newFile("/mnt/sdcard/"+packageName+".apk");
out.createNewFile();
FileInputStreamfis=newFileInputStream(in);
FileOutputStreamfos=newFileOutputStream(out);
intcount;
byte[]buffer=newbyte[256*1024];
while((count=fis.read(buffer))>0)
{
fos.write(buffer,0,count);
}
fis.close();
fos.flush();
fos.close();
}
java如何實現在web工程中用OpenOffice生成帶有圖片水印的pdf
需要itext2.1.5,
以下是對pdf加水印的代碼,包括文字水印和圖片水印
publicintfileCopy(StringsrcPath,StringdestPath){ FileOutputStreamfos=null; FileInputStreamfis=null; try{ fos=newFileOutputStream(destPath); fis=newFileInputStream(srcPath); byte[]buffer=newbyte[1024]; intlen=0; while((len=fis.read(buffer))>0){ fos.write(buffer,0,len); } return1; }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }finally{ try{ fis.close(); fos.flush(); fos.close(); }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } return0; } /** *為pdf文件加文字水印 * *@paramsrcPath *源文件路徑 *@paramdestPath *目標文件路徑 *@paramwaterText *水印文字 *@throwsDocumentException *@throwsIOException */ publicvoidwordWaterMark(StringsrcPath,StringdestPath,StringwaterText)throwsDocumentException,IOException{ intresult=fileCopy(srcPath,destPath); if(result==1){ //待加水印的文件 PdfReaderreader=newPdfReader(destPath); //加完水印的文件 PdfStamperstamper=newPdfStamper(reader,newFileOutputStream(srcPath)); inttotal=reader.getNumberOfPages()+1; PdfContentBytecontent; //設置字體 BaseFontbase=BaseFont.createFont(fontPath,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); //水印文字 intj=waterText.length();//文字長度 charc=0; inthigh=0;//高度 //循環對每頁插入水印 for(inti=1;i<total;i++){ //水印的起始 high=60; content=stamper.getUnderContent(i); PdfGStategs=newPdfGState(); gs.setFillOpacity(0.1f);//設置透明度為0.2 content.setGState(gs); //開始 content.beginText(); //設置顏色 //content.setColorFill(newColor()); //設置字體及字號 content.setFontAndSize(base,88); //設置起始位置 content.setTextMatrix(120,333); //開始寫入水印 for(intk=0;k<j;k++){ content.setTextRise(high); c=waterText.charAt(k); content.showText(c+""); high+=20; } content.endText(); } stamper.close(); System.out.println("添加成功++++++++++++++++++++++++++++++++++++++++++"); }else{ System.out.println("復制pdf失敗===================="); } } publicvoidpicWaterMark(StringsrcPath,StringdestPath,StringimageFilePath) throwsDocumentException,IOException{ intresult=fileCopy(srcPath,destPath); if(result==1){ //待加水印的文件 PdfReaderreader=newPdfReader(destPath); //加完水印的文件 PdfStamperstamper=newPdfStamper(reader,newFileOutputStream(srcPath)); Imageimg=Image.getInstance(imageFilePath); img.setAbsolutePosition(50,400);//坐標 img.setRotation(20);//旋轉弧度 img.setRotationDegrees(45);//旋轉角度 //image.scaleAbsolute(200,100);//自定義大小 img.scalePercent(50);//依照比例縮放 intpageSize=reader.getNumberOfPages(); for(inti=1;i<=pageSize;i++){ PdfContentByteunder=stamper.getUnderContent(i); under.addImage(img); PdfGStategs=newPdfGState(); gs.setFillOpacity(0.2f);//設置透明度為0.2 under.setGState(gs); } stamper.close();//關閉 System.out.println("添加成功++++++++++++++++++++++++++++++++++++++++++"); }else{ System.out.println("復制pdf失敗===================="); } }
linux下轉pdf可以用libreoffice,需要安裝,這個是免費的,具體代碼如下:
Stringcommand="libreoffice5.0--invisible--convert-topdf:writer_pdf_Export--outdir"+destFilepath +""+source; try{ p=Runtime.getRuntime().exec(command); p.waitFor(); }catch(InterruptedExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }
byte[]數組如何轉換成fileInputStream
如果必須要用FileOutputStream的話那是沒有辦法的,因為FileOutputStream是屬于比較底層的流,所有的構造方法都與文件關聯。
但是如果要寫入blob中的話使用FileOutputStream卻是有點兒多余的,因為像你那樣寫入文件讀出來之后同樣還是byte數組,所以可以直接使用OutputStream的write(byte[]b,intoff,intlen)方法,OFF開始標記一般設為0,len偏移量一般設為byte的length大小
創建個文件夾的multipartfile
創建一個文件夾的multipartfile需要使用Java中的MultipartFile類。該類可以通過構造函數創建一個multipartfile對象,然后使用該對象的方法來設置文件夾的名稱、路徑和內容等屬性。
在設置文件夾內容時,可以使用Java中的File類來創建一個空的文件夾,然后將其轉換為byte數組,并使用multipartfile對象的setContent方法將其設置為文件夾的內容。
最后,將multipartfile對象保存到磁盤上即可完成文件夾的創建。需要注意的是,multipartfile對象只能保存單個文件或文件夾,如果需要創建多個文件夾,則需要使用循環來實現。
關于本次fileoutputstream和bufferedreader讀取大文件的問題分享到這里就結束了,如果解決了您的問題,我們非常高興。