Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java对象未写入文件触发FileNotFoundException

当我试图写入文件时,它不想写入新文件。当应用程序尝试写入文件时,会发生FileNotFoundException。我在这里看到了这段代码:https://www.youtube.com/watch?v=YzwiuRDgSSY

package com.example.serialization;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.UUID;

import 安卓.support.v7.app.ActionBarActivity;
import 安卓.support.v7.app.ActionBar;
import 安卓.support.v4.app.Fragment;
import 安卓.os.Bundle;
import 安卓.view.LayoutInflater;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.TextView;
import 安卓.os.Build;

public class MainActivity extends ActionBarActivity {

TextView label;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Person arthur = new Person();
    arthur.name = "Arthur Dent";
    arthur.age = 44;

    label = (TextView) findViewById(R.id.textView1);
    label.setText("Nothing here");
    String fileName = "data.txt";
    try {
        ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(fileName));
        os.writeObject(arthur);
        os.close(); 
    } 
    catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        label.setText("File not found");
    } 
    catch (IOException e) {
        // TODO Auto-generated catch block
        label.setText("IOException");
        e.printStackTrace();
    }


    try {
        ObjectInputStream is = new ObjectInputStream(new FileInputStream(fileName));
        Person p = (Person) is.readObject(); // read object

        label.setText("Show name: " + p.name + " Show age: " + p.age);
        is.close();

    } 
    catch (StreamCorruptedException e) {
        label.setText("Stream corrupted");
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    catch (FileNotFoundException e) {
        label.setText("File not found 2");
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    catch (IOException e) {
        label.setText("IOException");
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    catch (ClassNotFoundException e) {
        label.setText("Class not found");
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

}

人。爪哇

package com.example.serialization;

import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Person implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 2794858669568274884L;
public String name;
public int age;

//streams are not Serializable

public ObjectOutputStream os;
}

共 (0) 个答案