我刚开始使用带有lpthw的python并且有一个关闭文件的特定问题.
我可以打开一个文件:
input = open(from_file)
indata = input.read()
#Do something
indata.close()
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试将代码简化为一行:
indata = open(from_file).read()
Run Code Online (Sandbox Code Playgroud)
如何关闭我打开的文件,或者它已经自动关闭?
在此先感谢您的帮助!
public static String[] words = null;
public static String readFile(String name) {
int i = 0;
try {
BufferedReader br = new BufferedReader(new FileReader(name));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
i++;
sb.append(sb.toString());
sb.append("\n");
line = br.readLine();
}
String everything = sb.toString();
words = everything.split("\\n");//not sure if this is right...
} finally {
br.close();
}
} catch (Exception e) {
e.getMessage();
}
return "Loaded " + i + " words";
} …Run Code Online (Sandbox Code Playgroud) 我想从我的文件中删除一行(特别是第二行),所以我使用了另一个文件进行复制,但使用下面的代码,第二个文件包含完全相同的文本.(我的原始文件.txt和我的最终文件.XML)
public static File fileparse() throws SQLException, FileNotFoundException, IOException {
File f=fillfile();//my original file
dostemp = new DataOutputStream(new FileOutputStream(filetemp));
int lineremove=1;
while (f.length()!=0) {
if (lineremove<2) {
read = in.readLine();
dostemp.writeBytes(read);
lineremove++;
}
if (lineremove==2) {
lineremove++;
}
if (lineremove>2) {
read = in.readLine();
dostemp.writeBytes(read);
}
}
return filetemp;
}
Run Code Online (Sandbox Code Playgroud) 我在Java中循环一个目录,试图读取每个文件.该目录包含一些拒绝访问的svn文件.我可以测试文件名并跳过svn文件,但如果我发现其他文件被拒绝访问,这个解决方案将不健壮.我试过这个:
for(File f : dir.listFiles()){
if(f.canRead()){
System.out.println("Trying " + f.getAbsolutePath());
try{
Scanner sc = new Scanner(f);
}
catch(IOException e){
e.printStackTrace();
}
continue;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我到达.svn文件时,它不会跳过; 我得到这个打印输出:
Trying C:\dir\.svn
java.io.FileNotFoundException: dir\.svn (Access is denied)
Run Code Online (Sandbox Code Playgroud)
canRead(),canWrite()和canExecute()所有人都有同样的问题.除了canXXX()我还可以用来跳过被拒绝的访问文件吗?
我在java中有这行代码:
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(name, append), "UTF-8"));
Run Code Online (Sandbox Code Playgroud)
这个编写器不写UTF-8文件,因为当我在notepad ++中打开它时,它说编码是:ANSI为UTF-8.我需要它是纯UTF-8.
你有什么建议吗?
FILE *fp;
fp=fopen("c:\\test.txt", "r");
int fgetc (FILE *fp);
int fputc( int c, FILE *fp );
Run Code Online (Sandbox Code Playgroud)
有没有办法更改计算机中已存在的文件名?如果是,那么我该如何引用该文件?使用指针我们只能引用文件的内容...所以有没有引用文件名的方法??? 这是我如何引用C中的文件:
这有效:
file.open("Levels\\test.txt");
Run Code Online (Sandbox Code Playgroud)
这不是:
string pathname = "Levels\\test.txt";
file.open(pathname);
Run Code Online (Sandbox Code Playgroud)
它输出以下错误:
no matching function for call to 'std::basic_ifstrea<char, std::char_traits<char> >::open
(std::string&)'
Run Code Online (Sandbox Code Playgroud) 我正在尝试将图像文件加载到缓冲区中以便通过scket发送它.我遇到的问题是程序创建一个有效大小的缓冲区,但它不会将整个文件复制到缓冲区中.我的代码如下
//imgload.cpp
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main(int argc,char *argv){
FILE *f = NULL;
char filename[80];
char *buffer = NULL;
long file_bytes = 0;
char c = '\0';
int i = 0;
printf("-Enter a file to open:");
gets(filename);
f = fopen(filename,"rb");
if (f == NULL){
printf("\nError opening file.\n");
}else{
fseek(f,0,SEEK_END);
file_bytes = ftell(f);
fseek(f,0,SEEK_SET);
buffer = new char[file_bytes+10];
}
if (buffer != NULL){
printf("-%d + 10 bytes allocated\n",file_bytes);
}else{
printf("-Could not allocate memory\n");
// Call …Run Code Online (Sandbox Code Playgroud) MSSQL数据库的性能是否会随着其中的更多表而下降?即使这些表包含很少甚至没有数据(即可能只有100行),但是有数百个表,每天可能增加数千个?
正在使用的MSSQL的特定版本是2008标准版.
这里的逻辑是,每个在网站上注册的用户都会获得自己的一组表,用于存储与所有其他用户分开的数据.这将允许用户帐户在服务器之间移动并完全删除,如果需要留下很少甚至没有痕迹.另外,为用户提供更多的舒适度,因为他们知道他们的数据是完全分段的,因此更难以获得.
另一个问题是用户帐户数据存储在类似于垂直数据库的表中,因为有一个内容数据表,其中只有几个字段,但这些条目可以应用于任何类型的数据,从小帖子,日历条目,私人消息和用户编写的大型多页文章.单个内容平均可以在该表中添加10到15行.因此,在我想象的场景中,为所有用户提供单个大表似乎是非常差的性能.
数据只能通过为站点制作的api访问,该API会提取特定于用户的数据,并验证每次呼叫的数据访问.可以在将来制作任何类型的数据,这就是为什么用户具有内容表以及其他几个用于设置等的原因.每个用户大约有15个表,用于网站的各种管理方面.例如,api允许您以用户z的形式从用户y请求内容块x.它验证您的安全性并从表中提取块x的所有数据字段.
因此,不断增加的大量表格最终会使系统大幅减速吗?或者在单个表中拥有如此大量的数据会是一个更大的减速?
我正在研究一个C#脚本,它必须在运行时访问一个随机文件,问题是文件是由其他来源动态生成的,我无法知道他们的名字,我已经解决了第一个问题是获取我的工作目录中有多少文件:
s = @"C:\Imagenes";
System.IO.DirectoryInfo d = new System.IO.DirectoryInfo(s);
int files;
files = d.GetFiles().Length;
Debug.Log(files.ToString());
return files;
Run Code Online (Sandbox Code Playgroud)
现在我想在我的工作指南中访问一个随机元素,但由于我不知道他们的名字是什么,有没有办法通过索引或其他东西得到他们的名字?
file-io ×10
java ×4
c++ ×2
arrays ×1
c ×1
c# ×1
file ×1
image ×1
performance ×1
permissions ×1
python ×1
sql-server ×1
utf-8 ×1