Implementation code list of java.io.File

Home | Back

File.java public class File implements Serializable, Comparable {

public File(File dir, String name) { this(dir == null ? null : dir.getPath, name); }

public File(String path) { this.path = fixSlashes(path); }

public File(String dirPath, String name) { if (name == null) { throw new NullPointerException; }       if (dirPath == null || dirPath.isEmpty) { this.path = fixSlashes(name); } else if (name.isEmpty) { this.path = fixSlashes(dirPath); } else { this.path = fixSlashes(join(dirPath, name)); }   }

public File(URI uri) { checkURI(uri); this.path = fixSlashes(uri.getPath); }

public boolean createNewFile throws IOException { FileDescriptor fd = null; try { // On Android, we don't want default permissions to allow global access. fd = Libcore.os.open(path, O_RDWR | O_CREAT | O_EXCL, 0600); return true; } catch (ErrnoException errnoException) { if (errnoException.errno == EEXIST) { // The file already exists. return false; }           throw errnoException.rethrowAsIOException; } finally { IoUtils.close(fd); // TODO: should we suppress IOExceptions thrown here? }   }

public boolean isFile { try { return S_ISREG(Libcore.os.stat(path).st_mode); } catch (ErrnoException errnoException) { // The RI returns false on error. (Even for errors like EACCES or ELOOP.) return false; }   } }

IFile.car module {   interface IFile { CreateNewFile(           [out] Boolean* succeeded);

IsFile(           [out] Boolean* isFile); } }

Elastos.IO.car module {   importlib("Elastos.Core.eco");

merge("../../../car/Elastos/IO/IFile.car");

class CFile { constructor(           [in] IFile* dir,            [in] String name);

constructor(           [in] String path);

constructor(           [in] String dirPath,            [in] String name);

constructor(           [in] IURI* uri);

interface IFile; } }

sources file for Elastos.IO.car TARGET_NAME= Elastos.IO TARGET_TYPE= eco

INCLUDES += $(MAKEDIR)/../../../inc/Elastos/common; INCLUDES += $(MAKEDIR)/../../../inc/Elastos/IO;

IMPORTS = Elastos.Core.eco IMPORTS += Elastos.Utility.eco IMPORTS += Com.Kortide.Platform.eco

SOURCES += File.cpp SOURCES += CFile.cpp

LIBRARIES = $(XDK_USER_LIB)/Elastos.Runtime.lib LIBRARIES += $(XDK_USER_LIB)/Elastos.Utility.lib LIBRARIES += $(XDK_USER_LIB)/Elastos.Core.lib ifeq "$(XDK_TARGET_PLATFORM)" "win32" LIBRARIES += $(XDK_LIB_PATH)/Elastos.Runtime.lib else LIBRARIES += $(XDK_TARGETS)/Elastos.Runtime.eco LIBRARIES += $(XDK_TARGETS)/Elastos.Utility.eco LIBRARIES += $(XDK_USER_LIB)/crt0.lib endif

File.h
 * 1) ifndef __FILE_H__
 * 2) define __FILE_H__


 * 1) include 
 * 2) include 
 * 3) include "Elastos.IO_server.h"
 * 4) include 
 * 5) ifdef _linux
 * 6) include 
 * 7) endif

using namespace Elastos;

class File { public: File;

virtual ~File;

virtual CARAPI_(PInterface) Probe(       /* [in] */ REIID riid) = 0;

CARAPI Init(       /* [in] */ IFile* dir,        /* [in] */ const String& name);

CARAPI Init(       /* [in] */ const String& path);

CARAPI Init(       /* [in] */ const String& dirPath,        /* [in] */ const String& name);

CARAPI Init(       /* [in] */ IURI* uri);

virtual CARAPI CreateNewFile(       /* [out] */ Boolean* succeeded);

virtual CARAPI IsFile(       /* [out] */ Boolean* isFile);

public: static const Char32 separatorChar; static const String separator; static const Char32 pathSeparatorChar; static const String pathSeparator;

private: const static Int64 serialVersionUID; String mPath; }

File.cpp File::File {}

File::~File {}

ECode File::Init(   /* [in] */ IFile* dir,    /* [in] */ const String& name) {   if (dir == NULL) { return Init(String(NULL), name); }   else { String dirPath; dir->GetPath(&dirPath); return Init(dirPath, name); } }

ECode File::Init(   /* [in] */ const String& path) {   return _Init(path); }

ECode File::Init(   /* [in] */ const String& dirPath,    /* [in] */ const String& name) {   if (name.IsNull) { return E_NULL_POINTER_EXCEPTION; }   if (dirPath.IsNullOrEmpty) { return _Init(name); }   else if (name.IsEmpty) { return _Init(dirPath); }   else { String dest; FAIL_RETURN(Join(dirPath, name, &dest)); return _Init(dest); } }

ECode File::Init(   /* [in] */ IURI* uri) {

FAIL_RETURN(CheckURI(uri)); String path;

return _Init(path); }

ECode File::_Init(   /* [in] */ const String& dirtyPath) {   // Cache the path and the absolute path. // We can't call isAbsolute here (http://b/2486943). String cleanPath; FixSlashes(dirtyPath, &cleanPath); Boolean isAbsolute = cleanPath.GetLength > 0 && cleanPath.GetChar(0) == (Int32)separatorChar; if (isAbsolute) { mPath = cleanPath; mAbsolutePath = cleanPath; }   else { String userDir; if (cleanPath.IsEmpty) { mAbsolutePath = userDir; }       else { String temp; Join(userDir, cleanPath, &temp); mAbsolutePath = temp; }       // We want path to be equal to cleanPath, but we'd like to reuse m_absolutePath's char[]. mPath = mAbsolutePath.Substring(               mAbsolutePath.GetLength - cleanPath.GetLength); }   return NOERROR; }

ECode File::CreateNewFile(   /* [out] */ Boolean* succeeded) {   assert(succeeded != NULL);

if (mPath.IsEmpty) { return E_IO_EXCEPTION; }   if (mAbsolutePath.IsNull) { *succeeded = FALSE; return NOERROR; }

// On Android, we don't want default permissions to allow global access. Int32 fd = open((const char*)mAbsolutePath, O_CREAT | O_EXCL, 0600); if (fd != -1) { // We created a new file. Success! *succeeded = TRUE; return NOERROR; }   if (errno == EEXIST) { // The file already exists. *succeeded = FALSE; return NOERROR; }

return E_IO_EXCEPTION; }

ECode File::IsFile(   /* [out] */ Boolean* isFile) {   assert(isFile != NULL); if (mPath.IsEmpty) { *isFile = FALSE; return NOERROR; }

struct stat sb; *isFile = (DoStat(mAbsolutePath, sb) && S_ISREG(sb.st_mode)); return NOERROR; }

CFile.h
 * 1) ifndef __CFILE_H__
 * 2) define __CFILE_H__


 * 1) include "_CFile.h"
 * 2) include "File.h"

CarClass(CFile), public File { public: CARAPI_(PInterface) Probe(       /* [in] */ REIID riid);

CARAPI CreateNewFile(       /* [out] */ Boolean* succeeded);

CARAPI IsFile(       /* [out] */ Boolean* isFile);

CARAPI constructor(       /* [in] */ IFile* dir,        /* [in] */ const String& name);

CARAPI constructor(       /* [in] */ const String& path);

CARAPI constructor(       /* [in] */ const String& dirPath,        /* [in] */ const String& name);

CARAPI constructor(       /* [in] */ IURI* uri);

private: // TODO: Add your private member variables here. };


 * 1) endif // __CFILE_H__

CFile.cpp
 * 1) include "cmdef.h"
 * 2) include "CFile.h"

PInterface CFile::Probe(   /* [in] */ REIID riid) {   return _CFile::Probe(riid); }

ECode CFile::CreateNewFile(   /* [out] */ Boolean* succeeded) {   VALIDATE_NOT_NULL(succeeded);

return File::CreateNewFile(succeeded); }

ECode CFile::IsFile(   /* [out] */ Boolean* isFile) {   VALIDATE_NOT_NULL(isFile);

return File::IsFile(isFile); }

ECode CFile::constructor(   /* [in] */ IFile* dir,    /* [in] */ const String& name) {   VALIDATE_NOT_NULL(dir);

return File::Init(dir, name); }

ECode CFile::constructor(   /* [in] */ const String& path) {   return File::Init(path); }

ECode CFile::constructor(   /* [in] */ const String& dirPath,    /* [in] */ const String& name) {   return File::Init(dirPath, name); }

ECode CFile::constructor(   /* [in] */ IURI* uri) {   return File::Init(uri); }

sources file for CFile.cpp TARGET_NAME = testCFile TARGET_TYPE = ecx

IMPORTS = Elastos.IO.eco

SOURCES = test.cpp

LIBRARIES = $(XDK_USER_LIB)/Elastos.Runtime.lib ifeq "$(XDK_TARGET_PLATFORM)" "win32" LIBRARIES += $(XDK_LIB_PATH)/Elastos.Runtime.lib else LIBRARIES += $(XDK_TARGETS)/Elastos.Runtime.eco LIBRARIES += -lstdc++ endif

test.cpp
 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include "Elastos.IO2.h"

using namespace Elastos;

int main(int argc, char *argv[]) {   printf("==== File: %s, Line: %d ====\n", __FILE__, __LINE__); String fp = String("/data/data/com.elastos.runtime/elastos/filetest.txt"); AutoPtr file; CFile::New(fp, (IFile**)&file); printf("==== File: %s, Line: %d ====\n", __FILE__, __LINE__); Boolean succeeded; file->CreateNewFile(&succeeded); assert(succeeded == TRUE); printf("==== File: %s, Line: %d ====\n", __FILE__, __LINE__); Boolean isfile; file->IsFile(&isfile); printf("==== File: %s, Line: %d ====\n", __FILE__, __LINE__); return 0; }

sources file of test.cpp TARGET_NAME = testCFile TARGET_TYPE = ecx

IMPORTS = Elastos.IO.eco

SOURCES = test.cpp

LIBRARIES = $(XDK_USER_LIB)/Elastos.Runtime.lib ifeq "$(XDK_TARGET_PLATFORM)" "win32" LIBRARIES += $(XDK_LIB_PATH)/Elastos.Runtime.lib else LIBRARIES += $(XDK_TARGETS)/Elastos.Runtime.eco LIBRARIES += -lstdc++ endif

Home | Back