Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中保存和加载到/从文件的问题_Java_File_Map_Save_Sublist - Fatal编程技术网

在Java中保存和加载到/从文件的问题

在Java中保存和加载到/从文件的问题,java,file,map,save,sublist,Java,File,Map,Save,Sublist,我发布了一个类似的问题,它变得越来越混乱,这将更容易理解。在Runner()中被/注释掉的部分。您取消对此的注释,运行程序一次,检查控制台打印输出。然后/comment out/再次,并再次运行该程序,您将注意到子代码本={}返回一个空白/null;这是我的问题,为什么返回空值 import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.In

我发布了一个类似的问题,它变得越来越混乱,这将更容易理解。在Runner()中被/注释掉的部分。您取消对此的注释,运行程序一次,检查控制台打印输出。然后/comment out/再次,并再次运行该程序,您将注意到子代码本={}返回一个空白/null;这是我的问题,为什么返回空值

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Runner {

Map<Integer, String> decodebook = new HashMap<Integer, String>();
// List<Integer> numbers = new LinkedList<Integer>();
Map<String, List<Integer>> codebook = new HashMap<String, List<Integer>>();
Map<String, List<Integer>> subcodebook = new HashMap<String, List<Integer>>();
List<Integer> numbers = new LinkedList<Integer>();
List<Integer> otherNumbers = new LinkedList<Integer>();

public static void main(String[] args) throws IOException {
    new Runner();
}

public Runner() throws IOException  {

    /*numbers.add(6);
    numbers.add(156);
    numbers.add(363);
    numbers.add(336);
    numbers.add(26);
    numbers.add(61);

    otherNumbers.add(68);
    otherNumbers.add(78);
    otherNumbers.add(28);
    otherNumbers.add(668);
    otherNumbers.add(618);
    otherNumbers.add(686);
    otherNumbers.add(682);

    subcodebook.put("tony", numbers.subList(0, 2));
    subcodebook.put("alf", numbers.subList(3, 5));


    codebook.put("alf", numbers);
    codebook.put("tony", otherNumbers);
    decodebook.put(7898, "alf");
    decodebook.put(87576, "tony");

     saveStuff();*/


     loadBooks();
     System.out.println(" codebook" + codebook);
     System.out.println(" decodebook" + decodebook);
     System.out.println(" subCodebook" + subcodebook);
}

public void loadBooks() throws IOException {

     loadCodeBook("CodeBook");
    //System.out.println(codebook);

     loadsubCodeBook("subCodeBook");


     loadDeCodeBook("DecodeBook");
    // System.out.println(codebook);
    //System.out.println(decodebook);

}

public Map<String, List<Integer>> loadCodeBook(String filePath)
        throws IOException {
    HashMap<String, List<Integer>> codebook = null;
    InputStream is = null;
    try {
        is = new ObjectInputStream(new FileInputStream(filePath));
        codebook = (HashMap<String, List<Integer>>) ((ObjectInputStream) is)
                .readObject();
    } catch (Exception ex) {
    } finally {
        is.close();
    }
    return this.codebook = codebook;
}
public Map<String, List<Integer>> loadsubCodeBook(String filePath)
        throws IOException {
    HashMap<String, List<Integer>> subcodebook = null;
    InputStream is = null;
    try {
        is = new ObjectInputStream(new FileInputStream(filePath));
        subcodebook = (HashMap<String, List<Integer>>) ((ObjectInputStream) is)
                .readObject();
    } catch (Exception ex) {
    } finally {
        is.close();
    }
    return this.subcodebook = subcodebook;
}


public Map<Integer, String> loadDeCodeBook(String filePath)
        throws IOException {
    HashMap<Integer, String> decodebook = null;
    InputStream is = null;
    try {
        is = new ObjectInputStream(new FileInputStream(filePath));
        decodebook = (HashMap<Integer, String>) ((ObjectInputStream) is)
                .readObject();
    } catch (Exception ex) {
    } finally {
        is.close();
    }
    return this.decodebook = decodebook;
}

public void saveStuff() {
    try {
        saveCodeBook(codebook, "CodeBook");

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {

        saveDecodeBook(decodebook, "DecodeBook");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {

        savesubCodeBook(subcodebook, "subcodeBook");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public void saveCodeBook(Map<String, List<Integer>> obj, String filePath)
        throws IOException {
    OutputStream os = null;
    try {
        os = new ObjectOutputStream(new FileOutputStream(filePath));

        ((ObjectOutputStream) os).writeObject(obj);
    } catch (Exception ex) {
    } finally {
        os.close();
    }
}

public void savesubCodeBook(Map<String, List<Integer>> obj, String filePath)
        throws IOException {
    OutputStream os = null;
    try {
        os = new ObjectOutputStream(new FileOutputStream(filePath));

        ((ObjectOutputStream) os).writeObject(obj);
    } catch (Exception ex) {
    } finally {
        os.close();
    }
}

public void saveDecodeBook(Map<Integer, String> obj, String filePath)
        throws IOException {
    OutputStream os = null;
    try {
        os = new ObjectOutputStream(new FileOutputStream(filePath));

        ((ObjectOutputStream) os).writeObject(obj);
    } catch (Exception ex) {
    } finally {
        os.close();
    }
}
import java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.ObjectInputStream;
导入java.io.ObjectOutputStream;
导入java.io.OutputStream;
导入java.io.Serializable;
导入java.util.HashMap;
导入java.util.LinkedList;
导入java.util.List;
导入java.util.Map;
公开课跑者{
Map decodebook=newhashmap();
//列表编号=新建LinkedList();
Map codebook=newhashmap();
Map subcodebook=newhashmap();
列表编号=新建LinkedList();
List otherNumbers=新建LinkedList();
公共静态void main(字符串[]args)引发IOException{
新手();
}
公共运行程序()引发IOException{
/*增加(6);
增加(156);
增加(363);
增加(336);
增加(26);
增加(61);
增加(68);
增加(78);
增加(28);
其他数字。增加(668);
其他数字。增加(618);
其他数字。增加(686);
其他数字。增加(682);
子码本。put(“托尼”,数字。子列表(0,2));
子代码本。put(“alf”,数字。子列表(3,5));
码本。put(“alf”,数字);
码本。输入(“托尼”,其他数字);
decodebook.put(7898,“alf”);
decodebook.put(87576,“托尼”);
saveStuff()*/
负荷书();
System.out.println(“码本”+码本);
System.out.println(“decodebook”+decodebook);
System.out.println(“子码本”+子码本);
}
public void loadBooks()引发IOException{
载荷码本(“码本”);
//System.out.println(码本);
加载子码本(“子码本”);
loadDeCodeBook(“DecodeBook”);
//System.out.println(码本);
//System.out.println(解码本);
}
公共地图加载码本(字符串文件路径)
抛出IOException{
HashMap码本=null;
InputStream=null;
试一试{
is=新对象输入流(新文件输入流(文件路径));
codebook=(HashMap)((ObjectInputStream)是)
.readObject();
}捕获(例外情况除外){
}最后{
is.close();
}
返回this.codebook=codebook;
}
公共地图加载子代码簿(字符串文件路径)
抛出IOException{
HashMap subcodebook=null;
InputStream=null;
试一试{
is=新对象输入流(新文件输入流(文件路径));
子代码本=(HashMap)((ObjectInputStream)为)
.readObject();
}捕获(例外情况除外){
}最后{
is.close();
}
返回this.subcodebook=subcodebook;
}
公共地图loadDeCodeBook(字符串文件路径)
抛出IOException{
HashMap decodebook=null;
InputStream=null;
试一试{
is=新对象输入流(新文件输入流(文件路径));
decodebook=(HashMap)((ObjectInputStream)为)
.readObject();
}捕获(例外情况除外){
}最后{
is.close();
}
返回此。decodebook=decodebook;
}
公共void saveStuff(){
试一试{
存储码本(codebook,简称“码本”);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
存储解码书(decodebook,简称decodebook);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
savesubCodeBook(subcodebook,“subcodebook”);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
公共void存储码本(映射对象,字符串文件路径)
抛出IOException{
OutputStream os=null;
试一试{
os=新对象输出流(新文件输出流(文件路径));
((ObjectOutputStream)os.writeObject(obj);
}捕获(例外情况除外){
}最后{
os.close();
}
}
公共void存储子码本(映射对象,字符串文件路径)
抛出IOException{
OutputStream os=null;
试一试{
os=新对象输出流(新文件输出流(文件路径));
((ObjectOutputStream)os.writeObject(obj);
}捕获(例外情况除外){
}最后{
os.close();
}
}
公共void存储簿(映射对象、字符串文件路径)
抛出IOException{
OutputStream os=null;
试一试{
os=新对象输出流(新文件输出流(文件路径));
((ObjectOutputStream)os.writeObject(obj);
}捕获(例外情况除外){
}最后{
os.close();
}
}
}

尝试保存(和恢复)同一对象两次。试着记下重要的部分

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Runner {

    Map<Integer, String> decodebook = new HashMap<Integer, String>();
// List<Integer> numbers = new LinkedList<Integer>();
    Map<String, List<Integer>> codebook = new HashMap<String, List<Integer>>();
    Map<String, List<Integer>> subcodebook = new HashMap<String, List<Integer>>();
    List<Integer> numbers = new LinkedList<Integer>();
    List<Integer> otherNumbers = new LinkedList<Integer>();

    public static void main(String[] args) throws IOException {
        new Runner();
    }

    public Runner() throws IOException {

        numbers.add(6);
        numbers.add(156);
        numbers.add(363);
        numbers.add(336);
        numbers.add(26);
        numbers.add(61);

        otherNumbers.add(68);
        otherNumbers.add(78);
        otherNumbers.add(28);
        otherNumbers.add(668);
        otherNumbers.add(618);
        otherNumbers.add(686);
        otherNumbers.add(682);

        // ---------------------------------------------------------
        //
        //  the imprtant part
        // 
        // ------------------------------------------------------------
        subcodebook.put("tony", new ArrayList(numbers.subList(0, 2)));
        subcodebook.put("alf", new ArrayList(numbers.subList(3, 5)));

        codebook.put("alf", numbers);
        codebook.put("tony", otherNumbers);
        decodebook.put(7898, "alf");
        decodebook.put(87576, "tony");

        saveStuff();
        System.out.println(" codebook" + codebook);
        System.out.println(" decodebook" + decodebook);
        System.out.println(" subCodebook" + subcodebook);
        loadBooks();
        System.out.println(" codebook" + codebook);
        System.out.println(" decodebook" + decodebook);
        System.out.println(" subCodebook" + subcodebook);
    }

    public void loadBooks() throws IOException {

        loadCodeBook("CodeBook");
        //System.out.println(codebook);

        loadsubCodeBook("subcodeBook");

        loadDeCodeBook("DecodeBook");
        // System.out.println(codebook);
        //System.out.println(decodebook);

    }

    public Map<String, List<Integer>> loadCodeBook(String filePath)
            throws IOException {
        HashMap<String, List<Integer>> codebook = null;
        InputStream is = null;
        try {
            is = new ObjectInputStream(new FileInputStream(filePath));
            codebook = (HashMap<String, List<Integer>>) ((ObjectInputStream) is)
                    .readObject();
        } catch (Exception ex) {
        } finally {
            is.close();
        }
        return this.codebook = codebook;
    }

    public Map<String, List<Integer>> loadsubCodeBook(String filePath)
            throws IOException {
        HashMap<String, List<Integer>> subcodebook = null;
        InputStream is = null;
        try {
            is = new ObjectInputStream(new FileInputStream(filePath));
            subcodebook = (HashMap<String, List<Integer>>) ((ObjectInputStream) is)
                    .readObject();
        } catch (Exception ex) {
        } finally {
            is.close();
        }
        return this.subcodebook = subcodebook;
    }

    public Map<Integer, String> loadDeCodeBook(String filePath)
            throws IOException {
        HashMap<Integer, String> decodebook = null;
        InputStream is = null;
        try {
            is = new ObjectInputStream(new FileInputStream(filePath));
            decodebook = (HashMap<Integer, String>) ((ObjectInputStream) is)
                    .readObject();
        } catch (Exception ex) {
        } finally {
            is.close();
        }
        return this.decodebook = decodebook;
    }

    public void saveStuff() {
        try {
            saveCodeBook(codebook, "CodeBook");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {

            saveDecodeBook(decodebook, "DecodeBook");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {

            savesubCodeBook(subcodebook, "subcodeBook");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void saveCodeBook(Map<String, List<Integer>> obj, String filePath)
            throws IOException {
        OutputStream os = null;
        try {
            os = new ObjectOutputStream(new FileOutputStream(filePath));

            ((ObjectOutputStream) os).writeObject(obj);
        } catch (Exception ex) {
        } finally {
            os.close();
        }
    }

    public void savesubCodeBook(Map<String, List<Integer>> obj, String filePath)
            throws IOException {
        OutputStream os = null;
        try {
            os = new ObjectOutputStream(new FileOutputStream(filePath));

            ((ObjectOutputStream) os).writeObject(obj);
        } catch (Exception ex) {
        } finally {
            os.close();
        }
    }

    public void saveDecodeBook(Map<Integer, String> obj, String filePath)
            throws IOException {
        OutputStream os = null;
        try {
            os = new ObjectOutputStream(new FileOutputStream(filePath));

            ((ObjectOutputStream) os).writeObject(obj);
        } catch (Exception ex) {
        } finally {
            os.close();
        }
    }

}
import java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.ObjectInputStream;
导入java.io.ObjectOutputStream;
导入java.io.OutputStream;
导入java.io.Serializable;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.LinkedList;
导入java.util.List;
导入java.util.Map;
公开课跑者{
Map decodebook=newhashmap();
//列表编号=新建LinkedList();
Map codebook=newhashmap();
Map subcodebook=newhashmap();
列表编号=新建LinkedList();
List otherNumbers=新建LinkedList();
公共静态void main(字符串[]args)引发IOException{
新手();
}
公共运行程序()引发IOException{
增加(6);
增加(156);
增加(363);
增加(336);
增加(26);
增加(61);
增加(68);
增加(78);
增加(28);