0
我想寫我modelclass數據轉換成JSON文件將數據寫入文件,這裏是我的代碼當我試圖在安卓
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name:"
android:textSize="21dp"
android:id="@+id/txtName" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editName"
android:hint="Name"
android:paddingLeft="10dp"
android:textColorHint="@color/colorAccent"/>
<View
android:layout_width="wrap_content"
android:layout_height="25dp"></View>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="City:"
android:textSize="21dp"
android:id="@+id/txtCity" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editCity"
android:hint="City"
android:paddingLeft="10dp"
android:textColorHint="@color/colorAccent"/>
<View
android:layout_width="wrap_content"
android:layout_height="25dp"></View>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Mobile:"
android:textSize="21dp"
android:id="@+id/txtMobile" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:paddingLeft="10dp"
android:id="@+id/editMobile"
android:hint="Mobile"
android:textColorHint="@color/colorAccent"/>
<View
android:layout_width="wrap_content"
android:layout_height="25dp"></View>
<Button
android:id="@+id/btnInsert"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textAlignment="center"
android:text="Insert"
android:textSize="21dp"
android:textColor="#FFFFFF"
android:background="@color/colorAccent"/>
</LinearLayout>
在這裏,我的模型類,即學生。 java的
package model;
import java.io.Serializable;
/**
* Created by MY WORLD on 11/26/15.
*/
public class Student implements Serializable {
private int id;
private String name;
private String city;
private String phno;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhno() {
return phno;
}
public void setPhno(String phno) {
this.phno = phno;
}
@Override
public String toString() {
return "Student[id="+id+"name="+name+"city="+city+"phno="+phno+"]";
}
}
這裏是我的mainActivity.java
package com.domore.jsonserialization;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.google.gson.Gson;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import model.Student;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText name,city,mobile;
Button insert;
int id=1;
FileOutputStream fos=null;
BufferedOutputStream bos=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.editName);
city=(EditText)findViewById(R.id.editCity);
mobile=(EditText)findViewById(R.id.editMobile);
insert=(Button)findViewById(R.id.btnInsert);
insert.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Student std=new Student();
std.setId(id);
std.setName(name.getText().toString());
std.setCity(city.getText().toString());
std.setPhno(mobile.getText().toString());
Gson gson=new Gson();
String json=gson.toJson(std);
Log.e("name:",std.getName());
Log.e("json Data",""+json);
try{
fos=new FileOutputStream("D:\\Student.json");
byte b[]=json.getBytes();
fos.write(b);
fos.close();
}
catch (FileNotFoundException ffe){
ffe.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
finally {
try{
fos.close();
}
catch (IOException e){
e.printStackTrace();
}
}
}
}
我用了2錯誤,如下面
1錯誤
11-26 11:18:08.680 2608-2608/? W/System.err: java.io.FileNotFoundException: D:\Student.json: open failed: EROFS (Read-only file system)
11-26 11:18:08.680 2608-2608/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:456)
11-26 11:18:08.680 2608-2608/? W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
11-26 11:18:08.680 2608-2608/? W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
11-26 11:18:08.680 2608-2608/? W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
11-26 11:18:08.681 2608-2608/? W/System.err: at com.domore.jsonserialization.MainActivity.onClick(MainActivity.java:63)
第二個錯誤
FATAL EXCEPTION: main
11-26 11:18:08.682 2608-2608/? E/AndroidRuntime: Process: com.domore.jsonserialization, PID: 2608
11-26 11:18:08.682 2608-2608/? E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.FileOutputStream.close()' on a null object reference
11-26 11:18:08.682 2608-2608/? E/AndroidRuntime: at com.domore.jsonserialization.MainActivity.onClick(MainActivity.java:76)
11-26 11:18:08.682 2608-2608/? E/AndroidRuntime: at android.view.View.performClick(View.java:4756)
11-26 11:18:08.682 2608-2608/? E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19749)
11-26 11:18:08.682 2608-2608/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-26 11:18:08.682 2608-2608/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-26 11:18:08.682 2608-2608/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-26 11:18:08.682 2608-2608/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5221)
11-26 11:18:08.682 2608-2608/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-26 11:18:08.682 2608-2608/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-26 11:18:08.682 2608-2608/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-26 11:18:08.682 2608-2608/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
11-26 11:18:08.695 1238-1252/? W/ActivityManager: Force finishing activity com.domore.jsonserialization/.MainActivity
我該怎麼辦,請,建議我解決了這個錯誤
問題標題可以更好。 –