程序需要计算并显示指定的charector在文本文件中出现的次数.
目前总数为零.我不是,如果我应该使用不同的循环,我也尝试使用'for'循环.
// Hold user input and sum
String fileName; // Holds the name of the file
String letter; // Letter to search for in the file
int total = 0; // Holds the total number of characters in the file
// Get the name of the file and character from the user
fileName = JOptionPane.showInputDialog("Please enter the name of a file:");
letter = JOptionPane.showInputDialog("Please enter a letter contained in the string");
// Open the file for reading
File file …Run Code Online (Sandbox Code Playgroud) 我有以下脚本从表单中获取一些值,并在每次在表单中输入新值时附加到文本文件:
$filename = "nic.txt"; #Must CHMOD to 666, set folder to 777
$text = "\n" . str_pad($fname, 30) . "" . str_pad($lname, 30) . "" . str_pad($tdate, 20) . "" . str_pad($ydept, 30) . "" . str_pad($percentage, 0) . "%";
$fp = fopen ($filename, "a"); # a = append to the file. w = write to the file (create new if doesn't exist)
if ($fp) {
fwrite ($fp, $text);
fclose ($fp);
#echo ("File written");
}
else {
#echo ("File was …Run Code Online (Sandbox Code Playgroud) 我需要将file.pdf复制到Winforms应用程序中的AppData文件夹中,但由于这将被许多人使用,我需要找到一个通用路径
iFile.CopyTo("somethinghere...//AppData//Temp//file.pdf");
Run Code Online (Sandbox Code Playgroud)
并复制该文件.
我使用以下代码写入一个公共文件.
fs.appendFile('log.txt', str, function (err) {
console.log("error writing file");
});
Run Code Online (Sandbox Code Playgroud)
它是从多个函数调用同时调用的.它正确完成文件写入操作但仍然抛出错误"错误写入文件".
什么是编写/执行此代码的简洁方法.我希望每个调用等待I/O操作,直到已经写完函数完成工作.
在Perl上遇到一些麻烦(我是全新的).我在同一目录中有一个.txt文件.我打算复制文件,将其打印到stdout,向副本添加更多文本,并比较文件大小.这是我到目前为止:
#!/usr/local/bin/perl
use File::Copy;
copy("data.txt", "copyOfData.txt") or die "copy failed :(";
open (MYFILE, "data.txt") or die "open failed :(";
while (<MYFILE>) {
chomp;
print "$_\n";
}
$filesize = -s MYFILE;
print "MYFILE filesize is $filesize\n";
close (MYFILE);
open(MYCOPYFILE, ">>copyOfData.txt");
print MYCOPYFILE "\nextra data here blah blah blah\n";
$filesize = -s MYCOPYFILE;
print "MYCOPYFILE filesize is $filesize\n";
close (MYCOPYFILE);
Run Code Online (Sandbox Code Playgroud)
但是,我得到的输出如下:
MYFILE文件大小为28 MYCOPYFILE文件大小为28
肯定MYCOPYFILE大小应该大于MYFILE大小,因为我添加了额外的文本?我检查了两个文本文件,副本确实有最后的文本.
谢谢你的帮助!
我有以下代码块:
private void saveAs()
{
CDocument currentDocument=this.panelMain().openedDocuments().get(this.panelMain().openedDocuments().size()-1);
StyledDocument contents=currentDocument.getStyledDocument();
DefaultEditorKit kit=new DefaultEditorKit();
JFileChooser chooserSaveAs=new JFileChooser();
chooserSaveAs.setDialogTitle("Save as ...");
if(chooserSaveAs.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)
{
String strNewFilename=chooserSaveAs.getSelectedFile().getName();
BufferedOutputStream out;
try
{
out=new BufferedOutputStream(new FileOutputStream(strNewFilename));
kit.write(out,
contents,
contents.getStartPosition().getOffset(),
contents.getLength());
out.close();
}
catch(IOException | BadLocationException ex)
{
Logger.getLogger(CFrameMain.class.getName()).log(Level.SEVERE,
null,
ex);
}
}
}
Run Code Online (Sandbox Code Playgroud)
一旦执行,此代码不会生成任何异常,但我无法在任何地方找到磁盘上保存的文件(我使用Total Commander搜索本地磁盘).为什么没有生成文件?我目前正在使用Windows 7 Ultimate,我试图保存到已登录用户的桌面(因为可能存在访问冲突问题......)?
是否有单行将一个(不是很大的)文本文件的内容读入字符串?
我找到的最短的:
#include <string>
#include <fstream>
#include <streambuf>
std::ifstream t("file.txt");
std::string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
Run Code Online (Sandbox Code Playgroud)
(对于大文件,请注意它是非常低效的解决方案,因为它必须在从流中读取的每个新字符之后重新分配缓冲区.)
credit:@Tyler McHenry将整个ASCII文件读入C++ std :: string
System.IO.File.CreateText(@"C:\\ProgramData\\Password Manager\\pwd.dat");
Run Code Online (Sandbox Code Playgroud)
这使文件完美.
System.IO.File.WriteAllLines(@"C:\\ProgramData\\Password Manager\\pwd.dat", pwd);
Run Code Online (Sandbox Code Playgroud)
但是这一行会出错
"该进程无法访问文件'C:\ ProgramData\Password Manager\pwd.dat',因为它正由另一个进程使用."
有没有办法在写入之前关闭文件?
我正在阅读并附加到每次我的Python程序运行时正在打开和读取的文本文件.它基本上是一个字符串日志.问题是,当我最初写入空白文件时,我必须包括action.write('\n')以便下一行打印下一个字符串.
但是,当我下次运行Python程序时读取文件时,它会读取"\n"并将其连接到前一个元素,该元素将添加到列表中.
这是基本上发生的事情:
pc = ["121", "343", "565", "787"]
with open(r"C:\tt.txt", "r+") as act:
curr=[]
for row in act:
if row == '\n':
pass
else:
curr.append(row)
for i in pc:
act.write(i)
act.write("\n")
print curr
>> curr = ['121\n', '343\n', '565\n', '787\n', '121\n', '343\n', '565\n', '787\n', '121\n', '343\n', '565\n', '787\n', '121\n', '343\n', '565\n', '787\n', '121\n', '343\n', '565\n', '787\n']
Run Code Online (Sandbox Code Playgroud)
我真的很难过如何解决这个问题.
我正在尝试.wav使用以下方法在matlab中为语音信号(文件)添加噪声:
load handel.mat;
hfile= 'noisy.wav';
y = wavread('daveno.wav');
y = y + randn(size(y)) * (1/100);
wavwrite(y, Fs, hfile);
nsamples=Fs;
Run Code Online (Sandbox Code Playgroud)
这增加了噪声,但是,它消除了实际的语音口语,因此仅包含噪声.我是否需要乘以更大的数字,或者,有人可以建议一种方法来解决这个问题吗?