我有以下文件创建/编写代码:
val file = new PrintWriter(new FileWriter(new File(TMP_DIR, fileName), true))
file.write(getFirstRow(tableName))
Run Code Online (Sandbox Code Playgroud)
出于一些奇怪的原因,它不是写入我的文件,而是每次创建它.getFirstRow方法返回我想要附加到文件的字符串.出了什么问题?
在Linux上,open(filename, O_RDONLY)如果给定目录的名称而不是常规文件,则似乎成功(尽管后续read()调用似乎失败,这只是预期).
什么是检查您是否尝试打开或刚刚打开目录的最佳方法,以便使用'oops,错误的文件名'而不是'恐慌,我们有一个文件但读取不起作用'失败?
我试图逐行比较两个大文本文件(每个10GB)而不将整个文件加载到内存中.我使用了以下代码,如其他线程所示:
with open(in_file1,"r") as f1, open(in_file2,"r") as f2:
for (line1, line2) in zip(f1, f2):
compare(line1, line2)
Run Code Online (Sandbox Code Playgroud)
但似乎python无法逐行读取文件.我观察到运行代码时的内存使用率> 20G.我也试过用:
import fileinput
for (line1, line2) in zip(fileinput.input([in_file1]),fileinput.input([in_file2])):
compare(line1, line2)
Run Code Online (Sandbox Code Playgroud)
这个也尝试将所有内容加载到内存中.我在Centos 5.9上使用Python 2.7.4,并且我没有在代码中存储任何行.
我的代码出了什么问题?我应该如何更改它以避免将所有内容加载到RAM中?
如何在最短的行数中将N行的文件加载到字符串的ArrayList中.
这就是我所拥有的,任何人都有关于如何减少行数和对象的任何建议?
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class FileLoad {
public static void main(String[] args) throws IOException, FileNotFoundException {
List<String> hs = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader(args[0]));
String line;
while ((line = br.readLine()) != null) {
hs.add(line);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我想替换这个BASH表达式:
expr $COUNT + 1 > $COUNT_FILE
Run Code Online (Sandbox Code Playgroud)
用Python中的等价物.我想出来了:
subprocess.call("expr " + str(int(COUNT)+1) + " > " + COUNT_FILE, shell=True)
Run Code Online (Sandbox Code Playgroud)
或者(也许好一点):
subprocess.call("echo " + str(int(COUNT)+1) + " > " + COUNT_FILE, shell=True)
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?
根据您的输入:
def out_to_file(out_string, file_name, append='w'):
with open(file_name, append) as f:
f.write(out_string+'\n')
Run Code Online (Sandbox Code Playgroud) 我在学校有一个网络驱动器,我有能力正常读取和写入,但是当我使用java来获取现有的文本文件并尝试写入它时,我得到了以下异常:
java.io.FileNotFoundException: p:\CompSci_CheckIn_Name.txt (The process cannot access the file because it is being used by another process)
Run Code Online (Sandbox Code Playgroud)
我可以读得很好,但是当我尝试写它时,它会抛出一个例外.我可以写入桌面并从桌面读取所有内容,但是当我尝试使用网络驱动器时,它会放弃.我怎么能解决这个问题?
file = new File(directories[i], "CompSci_CheckIn_Name.txt");
readName = new BufferedReader(new FileReader(file));
userName = readName.readLine();
passed = true;
Run Code Online (Sandbox Code Playgroud)
write = new PrintWriter(file);
write.println(newUser);
write.flush();
userName = newUser;
write.close();
Run Code Online (Sandbox Code Playgroud)
我已经尝试过BufferedWriter没有运气,同样的结果.
我在Microsoft Visual Studio 2012中使用C#,我正在处理以下代码:
string source = "d:\\source.txt";
string newFile = "d:\\newFile.txt";
if(!File.Exists(newFile))
{
File.Create(newFile);
string content = File.ReadAllText(source);
File.AppendAllText(newFile,content);
}
Run Code Online (Sandbox Code Playgroud)
此代码成功创建了文件,但在编译时File.AppendAllText(newFile,content)会生成错误:
进程无法访问文件"d:\newFile.txt",因为它正被另一个进程使用.
为什么会这样?
我正在尝试读取一个带有12x12 ASCII迷宫的文本文件.但是,我在屏幕上看到的只是一个12x12的星号网格.
我在上一学期编写的CLI扫雷游戏中使用了类似的代码,它工作正常.我不知道我做了什么导致它不起作用......
码:
bool loadBoard(Tile board [][gridSize], string filename) {
ifstream hndl;
char isWall;
hndl.open(filename);
// Check that the file is opened
if (hndl.is_open()) {
for (int row = 0; row < gridSize; row++) {
for (int col = 0; col < gridSize; col++) {
hndl >> isWall;
if (isWall == '*')
board[row][col].wall = true;
cout << row << col << isWall << " ";
}
cout << endl;
}
}
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
文件maze.txt:
************
* * * …Run Code Online (Sandbox Code Playgroud) 我正在努力学习如何用Java读取和写入文件.
我有以下类将写入文件:
public class EconAppData implements Serializable {
private static final long serialVersionUID = 1432933606399916716L;
protected transient ArrayList<Favorite> favorites;
protected transient List<CatalogTitle> catalogLists;
protected transient int rangeMonthlySettings;
protected transient int rangeQuarterlySettings;
protected transient int rangeAnnualSettings;
EconAppData() {
favorites = new ArrayList<Favorite>();
catalogLists = new ArrayList<CatalogTitle>();
rangeMonthlySettings = 3;
rangeQuarterlySettings = 5;
rangeAnnualSettings = -1;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的阅读方法:
protected Object readData(String filename) {
Object result;
FileInputStream fis;
ObjectInputStream ois;
try {
fis = openFileInput(filename);
ois = new ObjectInputStream(fis);
result = ois.readObject(); …Run Code Online (Sandbox Code Playgroud) 我正在使用此代码提取文本文件的每一行的某些部分:
std::ifstream file( "infile.txt" );
std::string in1, out1;
int blockNumber = 0;
while( getline( file, in1 ) )
{
int n = 0;
int i = 0;
while( i <= blockNumber )
{
n = in1.find_first_of("(", n + 1);
i++;
}
out1 = in1.substr( n + 1, ( in1.find_first_of(")", n) - n - 1) );
ofstream fmatch ("solo_matches.txt",ios::out);
fmatch.close();
fmatch.open("solo_matches.txt");
fmatch << out1;
fmatch.close();
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行代码时,结果并不像我预期的那样.只有最后一个字符串被写入该文件.如果我改用它:
std::cout << out1 << std::endl;
Run Code Online (Sandbox Code Playgroud)
我得到了我需要的确切输出.我不明白有什么区别.