ฉันกำลังเขียนไฟล์ .json และฉันต้องการอ่านไฟล์นั้น แต่ปัญหาคือ เมื่อฉันพยายามอ่านไฟล์ทั้งหมดในรูปแบบสตริง มันจะเพิ่มช่องว่างก่อนและหลังอักขระทุกตัว และเนื่องจากอักขระพิเศษ ทำให้ไม่สามารถอ่าน json ได้
รูปแบบ Json คือ
[{"description1":"The ThinkerA bronze sculpture by Auguste Rodin. It depicts a man in sober\nmeditation battling with a powerful internal struggle.","description2":"Steve JobsFounder of Apple, he is widely recognized as a charismatic pioneer of\nthe personal computer revolution.","description3":"Justin BieberBorn in 1994, the latest sensation in music industry with numerous\nawards in recent years."}]
แต่มันให้คำตอบแปลกๆ เช่น [ { " d e s c r i p t i o n 1 " : " T h e .....
เพื่อตัดช่องว่างเพิ่มเติมที่ฉันอ้างถึงสิ่งนี้ แต่ก็ยังใช้งานไม่ได้: Java วิธีแทนที่ 2 ช่องว่างขึ้นไปด้วยช่องว่างเดียวในสตริงและลบช่องว่างนำหน้าเท่านั้น
ฉันใช้รหัสนี้
File folderPath = Environment.getExternalStorageDirectory();
File mypath=new File(folderPath, "description.json");
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = null;
reader = new BufferedReader(new FileReader(mypath));
char[] buf = new char[1024];
int numRead=0;
while((numRead=reader.read(buf)) != -1)
{
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
buf = new char[1024];
}
String response = fileData.toString();
สตริง "response" มีการตอบสนองที่แปลก
แล้วมีใครช่วยฉันได้บ้าง?
สำหรับการเขียนลงในไฟล์ฉันใช้:
FileOutputStream fos = new FileOutputStream(mypath);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeChars(response);
BufferedReader
มีวิธีการreadLine
ลองใช้ดูนะครับ - person Mikita Belahlazau   schedule 08.01.2013