这似乎是一个非常微不足道的问题,但我正在尝试将一个booleans 数组写入文件,然后将它们读回数组中.我可以验证文件是否正确创建:
true
false
true
false
false
false
Run Code Online (Sandbox Code Playgroud)
但是当我尝试回读它时,我最终得到一个完全充满假的数组.这是我的阅读代码:
bools = new boolean[bools.length];
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String temp;
int i = 0;
while (null != (temp = reader.readLine())) {
bools[i] = Boolean.parseBoolean(temp);
// output what you read
if (bools[i]) {
System.out.println("true!");
} else {
System.out.println("false!");
}
}
reader.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(BooleanFiles.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(BooleanFiles.class.getName()).log(Level.SEVERE, null, ex);
}
// now output the resulting array
for (int …Run Code Online (Sandbox Code Playgroud) [编辑]澄清这是一个不同的问题:我的问题是解析字符串加倍以使令牌接受它而没有这个问题的答案没有格式的Java Double to String转换符合我的标准.
我有一个以下格式的文件:
A B 10
A C 12
A D 8
B D 5
B E 2
...
Run Code Online (Sandbox Code Playgroud)
以下是将上述数据存储在arraylist中的代码.但是它只存储起始节点,终端节点,而不是成本.
List<Node1> list = new ArrayList<Node1>();
while ((line = bufferReader.readLine()) != null) {
String[] tokens = line.split(" ");
list.add(new Node1(tokens[0], tokens[1], tokens[2])); // Error at tokens[2]
}
Run Code Online (Sandbox Code Playgroud)
以下是我的Node1类
class Node1 {
String start, end;
double cost;
public Node1(String start, String end, double cost){
this.start = start;
this.end = end;
this.cost = cost;
}
public String getStartNode() { …Run Code Online (Sandbox Code Playgroud) 如果有这样的文本文件:
3 size of group
10 individual therapy time(min)
15 group therapy time(min)
25 new client probability percentage
500 individual therapy cost
300 group therapy cost
Run Code Online (Sandbox Code Playgroud)
有没有办法只读取文本行的第一个整数并跳转到其他行?
我尝试过的:
settingsFile >> groupSize >> trash >> trash >> trash;
settingsFile >> indTime >> trash >> trash >> trash;
settingsFile >> newClientProb >> trash >> trash >> trash >> trash;
settingsFile >> indTherapyCost >> trash >> trash >> trash;
settingsFile >> groupTherapyCost >> trash >> trash >> trash;
Run Code Online (Sandbox Code Playgroud)
在我看来,这不是一个阅读文本文件的好方法,因为如果我trash从文本文件中删除一个字符串,整个阅读将失败.
我正在尝试编写一个程序,将一个节点保存到.txt中,但是当我执行程序时,它编写的内容较少(有时甚至没有).这是代码:
public static void main(String[] args) {
GeneradorSeñal gs = new GeneradorSeñalSinusoidal("sgsg");
Double salida;
try{
FileOutputStream out = new FileOutputStream("out.txt");
OutputStreamWriter muestras = new OutputStreamWriter(out);
for(int i=0; i<500; i++){
salida=gs.getSalida();
muestras.write(String.valueOf(salida)+"\n");
System.out.println(i+"\t"+salida);
}
}catch(Exception e){
e.getStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
GeneradorSeñal和GeneradorSeñalSinusoidal是创造正弦曲线的类.gs.getSalida()返回正弦曲线的值,正弦曲线类运行良好,这不是问题.
我想从文本文件中读取后,从每个单词中找到第5个字母.我不知道我哪里错了.
进入文件路径后,我会看到一个弹出窗口
read.c已停止工作.
代码:
#include<stdio.h>
#include<conio.h>
int main(void){
char fname[30];
char ch[]={'\0'};
int i=0;
FILE *fp;
printf("enter file name with path\n");
gets(fname);
fp=fopen(fname,"r");
if(fp==0)
printf("file doesnot exist\n");
else{
printf("File read successfully\n");
do{
ch[i]=getc(fp);
if(feof(fp)){
printf("end of file");
break;
}
else if(ch[i]=='\n'){
putc(ch[4],stdout);
}
i++;
}while(1);
fclose(fp);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我在调用异步方法时遇到了一些麻烦.该方法在类中并加载文本文件.我想在应用程序启动时加载文本文件.但是,我无法从构造函数中调用异步方法.我在网上看过的大多数例子都经常从异步按钮事件中调用方法.
目前,我正在尝试从我的构造函数:
Task.Run(async () =>
{
this.categories = await GenerateCategories(numberOfCategories);
});
Run Code Online (Sandbox Code Playgroud)
尽管这样可行,但在调用此方法后,程序的其余部分仍会继续,并且使用"类别"执行的代码会导致崩溃(因为类别仍为空,因为任务尚未完成).
总而言之,从哪里以及如何最好地调用此方法?当用户按下按钮或任何东西时,我不希望它加载,我希望在执行任何更多代码之前填充"类别".
我正在使用C#并编写通用应用程序,因此加载文件必须是异步的.
我正在编写一个程序,从文件中读取字符串,将它们保存到"字符串缓冲区",然后连接这些字符串并将它们写入另一个文件.
#define _CRT_SECURE_NO_WARNINGS
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <stdio.h>
int main() {
FILE *f = fopen("Read.txt", "r");
char line[20];
char buff[15][20];
int i = 0;
while (fgets(line, 18, f)) {
strcpy(buff[i], line);
i++;
}
FILE *h = fopen("Out.txt", "w+");
for (int j = 0; j < i; ++j) {
char ct[4] = "smt";
strcat(buff[j], ct);
fputs(buff[j], h);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
文件内容Read.txt:
Lorem ipsum
dolor sit
amet
Run Code Online (Sandbox Code Playgroud)
预期输出(File Out.txt):
Lorem ipsumsmt
dolor sitsmt
ametsmt
Run Code Online (Sandbox Code Playgroud)
但是我在Out.txt中获得了什么:
Lorem ipsum
smtdolor …Run Code Online (Sandbox Code Playgroud) 我将一个文件对象作为参数传递给Python中的函数.在这个函数里面我想读取作为参数传递的文件的内容.
我是否能够从头开始读取文件,或者在将文件传递给当前函数之前从最后一行继续读取?
我必须从文件中读取一行进行一些计算并写入另一个文件.我做过这样的事情
fd= open("abc.txt","w")
for line in open("test.txt","r"):
Do something Here
fd.write(modifiedline)
Run Code Online (Sandbox Code Playgroud)
我通常使用open和for open进行逐行操作.我使用的上述方式是否可以使用,或者是否有更好的方式我们使用open和open一起使用.
我是学生,希望了解更多.任何帮助表示赞赏.
打印字符串(只有字符串)到文件的正确方法是什么?当我尝试以我所知的标准方式进行时,即:
def printToFile(o:Object,n:String) = try{
val pathToOutput = "..\\some\\parent\\directory\\"
val path = Paths.get(pathToOutput + n)
val b = new ByteArrayOutputStream()
val os = new ObjectOutputStream(b)
os.writeObject(o)
Files.write(path, b.toByteArray,
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING)
}catch{
case _:Exception => println("failed to write")
}
Run Code Online (Sandbox Code Playgroud)
它似乎总是在前面
’NULENQtSTXT
后面 的部分ENQt似乎有所不同.(如果我声明o一个Object或String,则无关紧要.)
这非常烦人,因为我想打印几个.dot-Strings(Graphviz),然后将生成的.dot文件批量处理为.pdf文件.然而,前置的废话迫使我打开每个.dot文件并手动删除它 - 这种方法违背了批量处理它们的目的.