2日前、比較的大きなJSONデータ(約300kb)を取得し、HTTPリクエストで保存する必要がありました。考えた後、取得したJSONデータをファイルの形式で保存し、使用するたびにファイルを読むことにしました。
これ以上苦労せずに、コードをアップロードするだけです。
以下は、コードのスクリーンショットです。記事の最後に、完成したコードファイルがダウンロードできるようになります。
ファイルの作成方法:
ファイルコンテンツの書き方:
ファイルを削除する方法:
テスト:
ファイルの作成、書き込みコンテンツ、削除について。自分の状況に応じていくつかの変更を加えることができます。
以下はコードクラスです。
パッケージcom.file.run; import java.io.bufferedreader; import java.io.file; import java.io.fileinputStream; Import java.io.fileoutputStream; Import java.io.ioexception; Import java.io.inputStreamReader; Import java.io.printwriter; xijuzi-o* @version 2016年7月8日10:38:49 am*/public class forfile {// prate path path path path rivate static string path = "d:// file path + name private static string filenametemp; boolean createfile(string filename、string filecontent){boolean bool = false; filenameTemp = filenameTemp = filenameTemp = filenAme+"。txt"; //ファイルパス+ファイルタイプファイル= newファイル(filenametemp); try {//ファイルが存在しない場合、新しいファイルを作成します。 true; system.out.println( "成功ファイル、ファイルは"+filenametemp); //ファイルを作成した後、writefilecontent(filenametemp、filecontent);}} catch(e.printstacktrace();} return bool;}/* write contatent content* @param filepath* @paparpath* @paperpath* @param @throws ioexception*/public static boolean writefilecontent(string filepath、string newsstr)throws ioexception {boolean bool = false; string filein = newsstr+"/r/n"; // newline string temp = ""; fileinputstream fis = null null; printwriter pw = null; try {file file = new file(filepath); //ファイルパス(ファイル名を含む)//入力ストリームにファイルを読み取りますfis = new fileinputStream(file); ISR = new inputStreamReader(fis); br = new BufferedReader(ISR); StringBuffer Buffer = new StringBuffer(); //(int i = 0;(temp = br.readline())!= null; i ++){buffer.append(temp); buffer.append(system.getProperty( "line.separator"));} buffer.append(filein); fos = new fileoutputstream(file); pw = new Printwriter(fos); pw.write(buffer.tostring()。tochararray()); pw.flush; bool = true; exceptione.printstacktrace();}最後に{//閉じることを忘れないでください(pw!= null){pw.close();} if(fos!= null){fos.close();} if(br!= null){br.close();} if(isr!= null) {fis.close();}} return bool;}/*** delete file* @param filename name* @return*/public static boolean delfile(string filename){boolean bool = fals; filenameTemp = path+filename+"。txt"。txt "; filenametemp); {if(file.exists()){file.delete(); bool = true;}} catch(exception e){// todo:handle exception} return bool;} public static void main(string [] args){uuid uuid = uuid.uuid.randomuid();生徒たちは、私が最も持ちたいと思っている虹を反映し、あなたが私の夢であるので、あなたと一緒に空に連れて行ってください」;}}上記は、編集者が紹介したJavaでファイルを作成してコンテンツを作成する方法です。それがあなたに役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!