Write a list, map object to a file in Java

Source: Internet
Author: User

Link Address: http://blog.sina.com.cn/s/blog_4a4f9fb50101p6jv.html

Recommendation: Chicken Claw female melon seeds male strange like who should reflect on the price of London prices continue to skyrocket XWVQUSRTG Personal Center post message Doudouhe's Blog Http://blog.sina.com.cn/dechuan608hewei [ Subscribe][ Mobile Subscription] Home Page Blog directory imageAbout MePersonal Information Doudouhe Micro Blog

Add Friends Send a note

Write a message Add Attention

    • Blog level:
    • Blog score:842
    • Blog visit:222,144
    • Focus on popularity :
    • Receive a fountain pen :
    • Gift Pen:0
    • Honorary badge:
Related Posts
  • The hottest girl in the Korean nightclub.

    Hi-korea

  • How to eat radish in winter to match ginseng

    China Meteorological Bureau

  • int and byte arrays in Java are transferred to each other

    Zlingh

  • About those unfamiliar with Android (Message, Bindservice, Handler

    Pentium like the Sea

  • 3 ways to get random numbers from Java

    Usbo

  • Javaserializable and Externalizable serialization deserialization detailed

    Arnold

  • Write data to a BLOB field in Oracle via Java

    Write and write a row

  • java specifies encoding to read and write files

    Small white

  • Transformers 4: Already and certainly will break the record

    Pride Feng Qing

  • Differences between heap and Stack in Java

    Little hands, son.

  • Conversion of JSON strings to Java objects

    Arnold

  • type conversion of int, string in Java

    Mystery Piggy

more >>Recommended Posts
  • 2015 scientific and Technological innovation Inventory (PHOTOS)

  • Merry Christmas, family Christmas big part

  • 2015 China Internet haha list of 2:

  • Who pulled the gap between the rich and the poor made in China?

  • The legendary curtain call, Shan Iwata and his Nintendogs

  • Homemade staple food easy to do--bacon with chopped green onion

  • Count 2015 of the most stunning and popular wedding eggs

  • The second "bottom tide" mutual

  • African desert "Elf Circle" triggers many guesses

  • White Shark beach stranding passers-by

    • Frog Adventure sitting on crocodile nose

    • Scenery of Beihai Silver Beach in Guangxi

    • El Salvador City Cage prison

    • Lion Sands Sanctuary Lodge

    • Divers feed giant hammerhead sharks

    • A hermit living in the Russian mountains

See more >>who has read this blog post?
  • WVQUSRTG0 minutes ago
  • Li Jianwei January 8
  • Xiao Wu classmate ... January 7
  • Tzoner_ from ... January 6
  • zhengkai215January 4
  • Dude-liJanuary 3
  • Road one January 3
  • Wanyang Fat December 30
  • Little Eichich December 29
  • Sue Little Wood December 29
  • SuxiDecember 23
  • Little Xu December 19
body font size: Big medium small Write a list, map object to a file in Java(2014-03-17 17:39:36) reproduced
Tags: it Category: JAVA
Java's IO package provides a way to write files and read files to a file. Well, let's see what's going on. Let's start by writing a method like writing a single object in a file. 

public void WriteObject () {
try {

hashmap<</span>string,string> map = new hashmap<</span>string,string> ();
Map.put ("name", "Foolfish");


FileOutputStream OutStream = new FileOutputStream ("E:/1.txt");
ObjectOutputStream ObjectOutputStream = new ObjectOutputStream (OutStream);

Objectoutputstream.writeobject (map);
Outstream.close ();
System.out.println ("successful");
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}

Here we will insert a map object into a TXT file. Java's IO package provides a file stream of object. The code is simple, let's take a look at the object from the file.

public void ReadObject () {
FileInputStream Freader;
try {
Freader = new FileInputStream ("E:/1.txt");
ObjectInputStream ObjectInputStream = new ObjectInputStream (Freader);
hashmap<</span>string,string> map = new hashmap<</span>string,string> ();
Map = (hashmap<</span>string, string>) objectinputstream.readobject ();

System.out.println ("The name is" + map.get ("name"));

} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}

The code is also very simple, we can read the object in the file using ObjectInputStream's ReadObject, and then follow the package to

The type of the image time to cast. The output is AA foolfish.

The above provides the deposit and reading of a single object. This method also applies to multiple different objects. Let's use the code to illustrate it. We also insert two different objects, a map, and a list.


public class Objecttofile {
public void WriteObject () {
try {

hashmap<</span>string,string> map = new hashmap<</span>string,string> ();
Map.put ("name", "Foolfish");

list<</span>string> list = new arraylist<</span>string> ();
List.add ("Hello");
List.add ("everyone");


FileOutputStream OutStream = new FileOutputStream ("E:/1.txt");
ObjectOutputStream ObjectOutputStream = new ObjectOutputStream (OutStream);

Objectoutputstream.writeobject (map);
Objectoutputstream.writeobject (list);
Outstream.close ();
System.out.println ("successful");
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}

public void ReadObject () {
FileInputStream Freader;
try {
Freader = new FileInputStream ("E:/1.txt");
ObjectInputStream ObjectInputStream = new ObjectInputStream (Freader);
hashmap<</span>string,string> map = new hashmap<</span>string,string> ();
Map = (hashmap<</span>string, string>) objectinputstream.readobject ();
arraylist<</span>string> list = new arraylist<</span>string> ();
List = (arraylist<</span>string>) objectinputstream.readobject ();
System.out.println ("The name is" + map.get ("name"));
System.out.println ("AA" + list.get (1));
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}


public static void Main (String args[]) {
Objecttofile of = new Objecttofile ();
Of.writeobject ();
Of.readobject ();
}
}

Write a list, map object to a file in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.