Tag: 文件

ReadAllLines的Stream对象?

存在一个File.ReadAllLines而不是Stream.ReadAllLines 。 using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Test_Resources.Resources.Accounts.txt")) using (StreamReader reader = new StreamReader(stream)) { // Would prefer string[] result = reader.ReadAllLines(); string result = reader.ReadToEnd(); } 有没有办法做到这一点,或者我必须手动循环逐行通过文件?

如何在Ruby中重命名文件?

这是我的.rb文件: puts "Renaming files…" folder_path = "/home/papuccino1/Desktop/Test" Dir.glob(folder_path + "/*").sort.each do |f| filename = File.basename(f, File.extname(f)) File.rename(f, filename.capitalize + File.extname(f)) end puts "Renaming complete." 这些文件从其初始目录移到.rb文件所在的位置。 我想现场重命名文件,而不移动它们。 有什么build议做什么?

从列表中随机select50个项目写入文件

到目前为止,我已经想出了如何导入文件,创build新文件,并随机化列表。 我无法从列表中随意select50个项目来写入文件? def randomizer(input,output1='random_1.txt',output2='random_2.txt',output3='random_3.txt',output4='random_total.txt'): #Input file query=open(input,'r').read().split() dir,file=os.path.split(input) temp1 = os.path.join(dir,output1) temp2 = os.path.join(dir,output2) temp3 = os.path.join(dir,output3) temp4 = os.path.join(dir,output4) out_file4=open(temp4,'w') random.shuffle(query) for item in query: out_file4.write(item+'\n') 所以如果总的随机文件是 example: random_total = ['9','2','3','1','5','6','8','7','0','4'] 我想要3个文件(out_file1 | 2 | 3),第一个随机集合为3,第二个随机集合为3,第三个随机集合为3(对于这个例子,但是我想创build的应该有50个) random_1 = ['9','2','3'] random_2 = ['1','5','6'] random_3 = ['8','7','0'] 所以最后的'4'将不会包括在内。 我如何从我随机select的列表中select50个? 更好的是,我怎样才能从原始列表中随机select50个?

JVM属性-Dfile.encoding = UTF8或UTF-8?

我想知道什么是Java虚拟机(JVM)属性的值设置我的文件编码为UTF-8。 我把-Dfile.encoding=UTF8或-Dfile.encoding=UTF-8 ?

检查Windowspath中是否存在可执行文件

如果我使用ShellExecute (或使用System.Diagnostics.Process.Start()在.net中)运行进程,则启动的文件名进程不需要是完整path。 如果我想开始记事本,我可以使用 Process.Start("notepad.exe"); 代替 Process.Start(@"c:\windows\system32\notepad.exe"); 因为direcotry c:\windows\system32是PATH环境variables的一部分。 我怎样才能检查PATH上是否存在一个文件,而不执行进程和parsingPATHvariables? System.IO.File.Exists("notepad.exe"); // returns false (new System.IO.FileInfo("notepad.exe")).Exists; // returns false 但我需要这样的东西: System.IO.File.ExistsOnPath("notepad.exe"); // should return true 和 System.IO.File.GetFullPath("notepad.exe"); // (like unix which cmd) should return // c:\windows\system32\notepad.exe 是否有一个预定义的类来完成BCL中的这个任务?

如何使用python unittest编写文件的函数unit testing

我有一个Python输出文件写入磁盘的function。 我想用Pythonunit testing模块为它编写一个unit testing。 我应该如何声明文件的平等? 如果文件内容不同于预期的差异列表,我希望得到一个错误。 与unix diff命令的输出一样。 有没有官方/推荐的方法呢?

Java – 如何找出文件名是否有效?

在我的Java应用程序中,我将文件重命名为String参数中提供的文件名。 有一种方法 boolean OKtoRename(String oldName, String newName) 基本上检查newName是否已经被其他文件占用,因为我不想埋葬现有的文件。 现在我想到,也许newNamestring不会表示一个有效的文件名。 所以我想添加这个检查方法: if (new File(newName).isFile()) { return false; } 这显然不是正确的方法,因为在大多数情况下,newFile还不存在,因此虽然是 OKtoRename,但函数返回false。 我想知道,是否有一个方法(我知道有没有java.io.File对象)像canExist() ? 或者我将不得不求助于正则expression式来确保newFilestring不包含无效字符(例如,?,*,“,:)?我想知道是否有一个隐藏在JDK的某个地方,可以告诉我,如果一个string可能表示一个有效的文件名。

如何在打开文件时设置默认展开?

在我的.vimrc我已经set foldmethod=syntax来启用折叠的方法等。但是,我不喜欢默认情况下,每次我打开一个文件,整个事情是折叠的。 有没有一种方法来启用foldmethod ,但打开文件时却打开了文件?

com.android.ddmlib.SyncException:打开的文件过多

当我尝试在我的设备上的Eclipse中运行我的应用程序时,我已经开始得到很多com.android.ddmlib.SyncException: Too many open files在控制台中com.android.ddmlib.SyncException: Too many open filesexception/错误com.android.ddmlib.SyncException: Too many open files 。 为什么这是什么意思? 我能做些什么来阻止呢?

在Perl中打开和读取文件的最佳方式是什么?

请注意 – 我不是在寻找打开/读取文件的“正确”方式,也不是每次打开/读取文件的方式。 我只是想找出大多数人使用什么方式,也许在同一时间学习一些新的方法:)* 我的Perl程序中一个非常常见的代码块是打开一个文件并读取或写入。 我已经看到了很多这样做的方式,而且我这几年来执行这个任务的风格已经改变了。 我只是想知道什么是最好的 (如果有最好的方法)是这样做的? 我曾经打开过这样的文件: my $input_file = "/path/to/my/file"; open INPUT_FILE, "<$input_file" || die "Can't open $input_file: $!\n"; 但是我认为这有错误陷阱的问题。 添加括号似乎可以修复错误陷印: open (INPUT_FILE, "<$input_file") || die "Can't open $input_file: $!\n"; 我知道你也可以将一个文件句柄分配给一个variables,所以不用像上面那样使用“INPUT_FILE”,我可以使用$ input_filehandle – 这样更好吗? 对于阅读文件,如果它很小,那么这个文件是否有什么问题呢? my @array = <INPUT_FILE>; 要么 my $file_contents = join( "\n", <INPUT_FILE> ); 或者你应该总是循环,就像这样: my @array; while (<INPUT_FILE>) […]