forked from javeme/cpp2java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.java
More file actions
250 lines (192 loc) · 4.15 KB
/
File.java
File metadata and controls
250 lines (192 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
public class File
{
//public File( long bufferSize);
//File(String path,String openMode);
//public File(String path="",String openMode="rb+", long bufferSize=BUFFER_SIZE);
//public ~File();
//private void init(String path,String openMode, long bufferSize);
//public void openFile(String path,String openMode);
//public virtual void writeBytes(const char buf[], long length);
//public virtual void writeLine(String value);
//public virtual void writeInt(int value);
//public int write(char buffer, intlength);//原始函数
//public virtual int readBytes(char buf[], long length);
//public virtual int readLine(String line);
//public virtual int readInt();
//public virtual long getSize();
//public virtual int readAll(char buf[], long bufSize);
//public virtual void seek( long offset);
//public virtual void flush();
//public virtual void close();
private FILE m_pFile;
private String m_strPath;
private long m_nBufferSize,m_nUsedBufLength;
private char buffer;
private boolean m_bCloseAble;
File( long bufferSize)
{
init("","",bufferSize);
}
/
File(String path,String openMode)
{
this.File(path,openMode,BUFFER_SIZE);
}
/
File(String path,String openMode, long bufferSize)
{
init(path,openMode,bufferSize);
}
~File()
{
close();
//delete[]buffer;
}
void init(String path,String openMode, long bufferSize)
{
m_bCloseAble=false;
m_pFile=null;
m_nBufferSize=bufferSize;
m_nUsedBufLength=0;
buffer=new char[m_nBufferSize];
if(path!="")
{
openFile(path,openMode);
}
}
void openFile(String path,String openMode)
{
//path="./root/"+path;
if(path=="")
throw IOException("文件名为空.");
if(m_pFile!=null)
close();
m_strPath=path;
m_pFile=fopen(path.c_str(),openMode.c_str());
if(m_pFile==null)
{
int pos=path.find_last_of("/");
if(pos<0)
pos=path.find_last_of("\\");
if(pos>0)
{
String folder=path;
folder.replace(pos,path.length()-pos,"");
if(mkdir(folder.c_str())==0)
m_pFile=fopen(path.c_str(),openMode.c_str());
}
}
if(m_pFile==null)
throw IOException("'"+path+"'文件打开失败.");
m_bCloseAble=true;
}
void writeBytes(const char buf[], long length)
{
if(m_nUsedBufLength<0)
{
throw IOException("写入文件异常.");
}
for(int i=0;i<length;i++)
{
buffer[m_nUsedBufLength++]=buf[i];
if(m_nUsedBufLength >= m_nBufferSize)
this.flush();
}
}
void writeLine(String value)
{
value.append("\n");
writeBytes( (char)value.c_str(), strlen(value.c_str()) );
}
void writeInt(int value)
{
char buf[sizeof(int)];
for(int i=0;i<sizeof(value);i++)
{
buf[i]=(value>>i8) 0xff;
}
writeBytes(buf,sizeof(int));
}
int readBytes(char buf[], long length)
{
int nReturn=fread(buf,sizeof(char),length,m_pFile);
if(nReturn!=0 nReturn!=length)
{
close();
throw IOException("文件读取出错.");
}
return nReturn;
}
int readLine(String line)
{
int readedLen=0,tmp;
char buf[BUFFER_SIZE]="a";
do
{
tmp=readBytes(buf[readedLen],1);
if(tmp>0)
readedLen+=tmp;
if(readedLen>BUFFER_SIZE)
throw IOException("缓冲已满.");
}while(tmp>0 buf[readedLen-1]!='\0' buf[readedLen-1]!='\n');
line=String(buf,readedLen);
return readedLen;
}
int readInt()
{
int value=0;
char buf[sizeof(int)];
this.readBytes(buf,sizeof(int));
for(int i=0;i<sizeof(int);i++)
{
value+=(buf[i] 0xff)<<8i;
}
return value;
}
long getSize()
{
long current=ftell(m_pFile);
fseek(m_pFile,0,SEEK_END);
long length=ftell(m_pFile);
//rewind(m_pFile);//重定位到开头
fseek(m_pFile,current,SEEK_SET);
return length;
}
int readAll(char buf[], long bufSize)
{
if(bufSize<getSize())
{
return -1;
}
return this.readBytes(buf,getSize());
}
void seek( long offset)
{
fseek(m_pFile,offset,SEEK_SET);
}
int write(char buffer, int length)
{
return fwrite(buffer,sizeof(char),length,m_pFile);
}
void flush()
{
if(m_nUsedBufLength==0)
return;
int nReturn=fwrite(buffer,sizeof(char),m_nUsedBufLength,m_pFile);
if(nReturn!=m_nUsedBufLength)
{
throw IOException("写入文件'"+m_strPath+"'出错.");
}
m_nUsedBufLength=0;
}
void close()
{
if(!m_bCloseAble)
return;
flush();
if(fclose(this.m_pFile))
throw IOException("关闭文件'"+m_strPath+"'出错.");
m_bCloseAble=false;
m_pFile=null;
}
}