我在Python中有一个浮点值列表:
floats = [3.14, 2.7, 0.0, -1.0, 1.1]
Run Code Online (Sandbox Code Playgroud)
我想使用IEEE 32位编码将这些值写入二进制文件.在Python中执行此操作的最佳方法是什么?我的列表实际上包含大约200 MB的数据,所以"不太慢"的东西是最好的.
由于有5个值,我只想要一个20字节的文件作为输出.
我一直在研究这段似乎微不足道的小代码,但我仍然无法确定问题出在哪里.我的功能很简单.打开文件,复制其内容,替换内部的字符串并将其复制回原始文件(然后在文本文件中进行简单的搜索和替换).我真的不知道怎么做,因为我在原始文件中添加了行,所以我只创建了一个文件副本,(file.temp)副本也备份(file.temp)然后删除原始文件(文件)并将file.temp复制到文件.我在删除文件时遇到异常.以下是示例代码:
private static bool modifyFile(FileInfo file, string extractedMethod, string modifiedMethod)
{
Boolean result = false;
FileStream fs = new FileStream(file.FullName + ".tmp", FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
StreamReader streamreader = file.OpenText();
String originalPath = file.FullName;
string input = streamreader.ReadToEnd();
Console.WriteLine("input : {0}", input);
String tempString = input.Replace(extractedMethod, modifiedMethod);
Console.WriteLine("replaced String {0}", tempString);
try
{
sw.Write(tempString);
sw.Flush();
sw.Close();
sw.Dispose();
fs.Close();
fs.Dispose();
streamreader.Close();
streamreader.Dispose();
File.Copy(originalPath, originalPath + ".old", true);
FileInfo newFile = new FileInfo(originalPath + ".tmp");
File.Delete(originalPath);
File.Copy(fs., originalPath, true); …Run Code Online (Sandbox Code Playgroud) 我启动程序时需要读取文本文件.我正在使用eclipse并启动了一个新的java项目.在我的项目文件夹中,我得到了"src"文件夹和标准的"JRE系统库"+ staedteliste.txt ...我只是不知道在哪里放文本文件.我真的尝试了我能想到的每个文件夹....我不能使用"硬编码"路径,因为文本文件需要包含在我的应用程序中...
我使用以下代码来读取文件,但是我收到此错误:
Error:java.io.FileNotFoundException:staedteliste.txt(No such file or directory)
Run Code Online (Sandbox Code Playgroud)
public class Test {
ArrayList<String[]> values;
public static void main(String[] args) {
// TODO Auto-generated method stub
URL url = Test.class.getClassLoader().getResource("src/mjb/staedteliste.txt");
System.out.println(url.getPath()); // I get a nullpointerexception here!
loadList();
}
public static void loadList() {
BufferedReader reader;
String zeile = null;
try {
reader = new BufferedReader(new FileReader("src/mjb/staedteliste.txt"));
zeile = reader.readLine();
ArrayList<String[]> values = new ArrayList<String[]>();
while (zeile != null) {
values.add(zeile.split(";"));
zeile = reader.readLine();
}
System.out.println(values.size());
System.out.println(zeile);
} catch (IOException …Run Code Online (Sandbox Code Playgroud) 由于我的Mac具有不区分大小写的文件系统案例,因此在本地运行测试时不会捕获相关的拼写错误,但是在运行Linux的构建服务器上它们会失败.
例如:在Lion上运行时require('./mymodule')会发现./myModule.js,但在Linux上运行时则不会.
因为我想让测试在本地失败以便不破坏服务器上的构建,所以我正在寻找一种方法来使node.js需要更严格,因为如果文件名是不精确(即套管有差异).
有谁知道这样做的方法?
编辑:
由于这个问题似乎没有很好的解决方案,因此我创建了valiquire.
此工具验证在整个nodejs项目中找到的所有需求,同时确保外壳正确.
我需要获得大小超过2 GB的文件的文件大小.(在4.6 GB文件上测试).没有外部程序有没有办法做到这一点?
当前状态:
filesize(),stat()并fseek()失败fread()和feof()工作通过读取文件内容可以获得文件大小(非常慢!).
$size = (float) 0;
$chunksize = 1024 * 1024;
while (!feof($fp)) {
fread($fp, $chunksize);
$size += (float) $chunksize;
}
return $size;
Run Code Online (Sandbox Code Playgroud)
我知道如何在64位平台上使用它(使用fseek($fp, 0, SEEK_END)和ftell()),但我需要32位平台的解决方案.
解决方案:我已经为此启动了开源项目.
Big File Tools是在PHP中操作超过2 GB的文件所需的hacks集合(即使在32位系统上).
嘿,我正在尝试打开一个文件,并从一个偏移量读取一定长度!我读了这个主题: 如何使用Java中的文件中的特定行号读取特定行? 在那里,它说不能读取某一行而不读取之前的行,但我想知道字节!
FileReader location = new FileReader(file);
BufferedReader inputFile = new BufferedReader(location);
// Read from bytes 1000 to 2000
// Something like this
inputFile.read(1000,2000);
Run Code Online (Sandbox Code Playgroud)
是否可以从已知偏移中读取某些字节?
如果我这样做:
File f = new File("c:\\text.txt");
if (f.exists()) {
System.out.println("File exists");
} else {
System.out.println("File not found!");
}
Run Code Online (Sandbox Code Playgroud)
然后创建文件并始终返回"文件存在".是否可以在不创建文件的情况下检查文件是否存在?
编辑:
我忘了提到它是在for循环中.所以这是真实的事情:
for (int i = 0; i < 10; i++) {
File file = new File("c:\\text" + i + ".txt");
System.out.println("New file created: " + file.getPath());
}
Run Code Online (Sandbox Code Playgroud) 显然,可以使用fgetl或类似函数循环遍历文件并递增计数器,但有没有办法确定文件中的行数而不进行这样的循环?
Python的os模块包含一个特定于平台的行分隔字符串的值,但是文档明确表示在写入文件时不使用它:
在编写以文本模式打开的文件时,不要将os.linesep用作行终止符(默认值); 在所有平台上使用单个'\n'代替.
以前的问题探讨了为什么你不应该在这种情况下使用它,但那么它有什么用处呢?什么时候应该使用行分隔符,为什么?