node.js 是否支持写入随机访问?
我看到这个包,但它不起作用(如果我写了两个或更多部分,文件已损坏)
我有以下 main() 程序 -
int main()
{
vector<vector<vector<int> > > vec;
vector<vector<int> > vv;
vector<int> ve;
//fill vectors
for (int i = 0; i < 5; i++)
{
//vector<vector<int> > vv;
for (int j = 0; j < 10; j++)
{
//vector<int> ve;
for (int t = 0; t < 3; t++)
{
ve.push_back(t);
}
vv.push_back(ve);
}
vec.push_back(vv);
}
for (vector<vector<vector<int> > >::iterator it = vec.begin(); it != vec.end(); it++)
{
for (vector<vector<int> >::iterator it2 = vv.begin(); it2 != vv.end(); …Run Code Online (Sandbox Code Playgroud) 我创建了一个随机访问文件,并在其中写入了一些信息。我想将随机访问文件中每一行的位置存储在一个数组中。我将有一个指向随机访问文件每一行的索引。所以我需要在我的索引中存储随机访问文件的行的位置。
我的程序如下
package randomaccessfile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class raf {
public static void main(String[] args) throws FileNotFoundException, IOException {
File file = new File("DocumentsFile.txt");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
for (int i = 0; i <= 10 ; i++) {
String sentence = "1 Info Name Surname"; //i create sentences in the random access file
raf.seek(file.length());
raf.writeBytes(sentence);
raf.writeBytes("\r\n");
}
raf.close();
}
}
Run Code Online (Sandbox Code Playgroud)
对于在随机访问文件中创建的每一行,我想将它们的位置存储在一个数组中。
然后这些位置将被存储在索引中。
有没有什么方法可以用来返回随机访问文件中一行的位置?
是否有一些PHP函数或类允许我读取像字符数组的文件?
例如:
$string = str_split('blabla');
$i = 0;
switch($string[$i]){
case 'x':
do_something();
$i++;
case 'y':
if(isset($string[++$i]))
do_something_else();
else
break;
case 'z':
// recursive call of this code etc..
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用$string = file_get_contents($file),但问题是我得到了一个用于800K小文件(如80MB)的大量内存.
所以,我可以以某种方式"上传"上面代码中的文件,类似于在调用isset()时自动从文件中读取数据的类访问?:)
据我说:
基于索引:可以通过传递索引来访问.现在内部它正在进行随机或顺序迭代并不重要.
随机访问:您可以随机访问一个位置.
顺序访问:从其他位置开始依次逐个访问所需位置. 但是在采访中我说LinkedList是基于java的索引,因为它提供了所有方法add(int index,obj),get(int index),remove(int index).人们不接受.然后我说基于索引和随机访问是两个不同的概念.我对吗?我想知道是否有人这个号码做了什么,我听说它必须用字节做什么?,我真的不知道..有谁可以请赐教:
public int total_reg() {
try {
int reg;
RandomAccessFile file = new RandomAccessFile(Ruta, "rw");
reg = (int) file.length();
file.close();
return (int) reg / 684; //<---HERE WHAT IS THIS
} catch (IOException e) {
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)