A portable electronic device of this invention has a control CPU (Central Processing Unit) and a data memory. The data memory is divided into a plurality of areas, and each area consists of a data area and an attribute area indicating an attribute of the data area. The attribute area consists of a first flag indicating whether or not data is stored in the data area, a second flag indicating whether or not data written in the data area is valid, and a third flag indicating whether or not data written in the data area is stored as a block. When an instruction supplied from a host system is a valid-data rearrangement instruction, the control CPU refers to the second flag, and sets a first flag of a data area whose second flag indicates that data is invalid to indicate that data is unwritten, thus rear-ranging valid data of the data memory so that no invalid data area is present between valid data areas. When the third flag indicates write incompletion, the control CPU sets the second flag to make the data area invalid.