이틀 전, 비교적 큰 JSON 데이터 (약 300KB)를 얻고 HTTP 요청을 통해 저장해야했습니다. 그것에 대해 생각한 후, 나는 마침내 얻은 JSON 데이터를 파일 형식으로 저장하고 사용할 때마다 파일을 읽기로 결정했습니다.
더 이상 고민하지 않고 코드를 업로드하십시오.
다음은 코드의 스크린 샷입니다. 기사 끝에 다운로드 할 수있는 완료된 코드 파일이 있습니다.
파일 작성 방법 :
파일 컨텐츠 작성 방법 :
파일 삭제 방법 :
시험:
파일 생성, 컨텐츠 작성, 삭제 자신의 상황에 따라 약간의 변경을 할 수 있습니다.
다음은 코드 클래스입니다.
package 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; import java.uid;/* emupor; java.uid;/* emuct; xijuzi-o* @version 2016 년 7 월 8 일 오전 10:38:49 AM*/public class forfile {// 파일 경로 생성 비공개 정적 문자열 path = "d : // file //"; // file path + name private static string filenametemp;/*** @param filename 파일 이름* @param filename 파일 컨텐츠* @param filename filenaTemp* @param filetic intatic이 성공할 것인지, @preation이 성공할 것인지, @param filename filenametemp*. CreateFile (String Filename, String FileContent) {boolean bool = false; filenametemp = path+filename+". txt"; // 파일 path+이름+파일 유형 파일 = 새 파일 (filenametemp); try {// 파일이 존재하지 않으면 새 파일을 만듭니다. true; system.out.println ( "파일 생성, 파일은"+filenametemp); // 파일을 작성한 후 writeFileContent (filenametemp, fileContent);}} catch (E.PrintStackTrace (E.PrintStackTrace (e.printStackTrace); ioException*/public static boolean writeFileContent (String FilePath, String NewsStr)는 ioException {boolean bool = false; string filein = newsstr+"/r/n"; // 새로 작성된 줄, Newline String temp = ""; fileInputStream fis = null; inputStreamreader isr = null; buttreader br. null; printwriter pw = null; try {file file = new File (FilePath); // 파일 경로 (파일 이름 포함) // 입력 스트림으로 파일을 읽습니다. isr = new inputStreamReader (fis); br = new bufferedReader (isr); stringBuffer buffer = new StringBuffer (); // 파일의 원래 내용 (int i = 0; buffer.append (system.getProperty ( "line.separator");} buffer.append (fileIn); fos = new FileOutputStream (file); pw = new printWriter (fos); pw.write (buffer.tostring (). tochararray ()); pw.flush ();} catch;} catch;} exceptione.printstacktrace ();} 마지막으로 {// if (pw! = null) {pw.close ();} if (fos! = null) {fos.close ();} if (br! = null) {br.close (); {isr. close (isr.}} if (isr.} if); {fis.close ();}} return bool;}/*** 파일 삭제* @param filename 파일 이름* @return*/public static boolean delfile (string filename) {boolean bool = false; filenametemp = path+filename+"; 파일 = 새 파일 (filenametemp); {if (file.exists ()) {file.delete (); bool = true;}} catch (예외 e) {// todo : handle rethow} return bool;} public static void main (string [] args) {uuid.randomuuid (); chreatefile (myfile) ", 내 꿈은"내 꿈 " 젖은 학생들, 내가 가장 많이 갖고 싶어하는 무지개를 반영하고, 당신이 내 꿈, 나의 꿈이기 때문에 나를 당신과 함께 하늘로 데려가십시오. ";}}위는 편집자가 소개 한 Java에서 파일을 작성하고 컨텐츠를 작성하는 방법입니다. 나는 그것이 당신에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!