因此,我在这个网站上看到了很多解决方案以及有关从 C++ 文本文件中读取内容的教程,但尚未找到解决我的问题的方法。我是 C++ 新手,所以我认为我在拼凑一些文档以理解这一切方面遇到了困难。
我想做的是读取文本文件编号,同时忽略文件中用“#”表示的注释。因此,示例文件如下所示:
#here is my comment
20 30 40 50
#this is my last comment
60 70 80 90
Run Code Online (Sandbox Code Playgroud)
当没有任何注释时,我的代码可以很好地读取数字,但我不明白如何很好地解析流以忽略注释。现在它是一种黑客解决方案。
/////////////////////// Read the file ///////////////////////
std::string line;
if (input_file.is_open())
{
//While we can still read the file
while (std::getline(input_file, line))
{
std::istringstream iss(line);
float num; // The number in the line
//while the iss is a number
while ((iss >> num))
{
//look at the number
}
}
}
else
{
std::cout << "Unable to open …Run Code Online (Sandbox Code Playgroud) 我在 Java 应用程序中遇到以下情况。
从数据库中检索此Blob对象,它是 DB 上 PDF 文件的表示形式:
Blob blobPdf = cedolinoPdf.getAllegatoBlob();
Run Code Online (Sandbox Code Playgroud)
现在我必须将其转换为 PDF 文件。我怎样才能完成这个任务?
总氮
我正在尝试替换所有使用 Files 和 FileReaders 的实例,并用 InputStreams 和适当的读取器替换它们,以准备将应用程序打包到 jar 中 - 但是,当我尝试使用ClassLoader.getSystemClassLoader().getResourceAsStream()File 方法设法找到的相同路径时,它由于找不到文件而失败。
我使用和输出的代码:
文件中的文本:
e:Easy
definitions/easy_level_definitions.txt
h:Hard
definitions/hard_level_definitions.txt
d:Derp
definitions/derp_level_definitions.txt
Run Code Online (Sandbox Code Playgroud)
代码:
String line;
File f = new File("src/resources/level_sets.txt");
BufferedReader reader1 = null;
try {
reader1 = new BufferedReader(new FileReader(f));
while ((line = reader1.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader1 != null) {
reader1.close();
}
} catch (IOException e) {
e.printStackTrace(); …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过 访问创建的文件夹FileInputStream,但 Tomcat 返回了异常。
java.io.FileNotFoundException:(directoy path) access denied.
Run Code Online (Sandbox Code Playgroud)
这是创建文件夹的代码。
String dirname = "Myfolder";
File dir = new File( dirname );
dir.mkdirs();
Run Code Online (Sandbox Code Playgroud)
我的问题是我无法通过 InputStream 访问此文件夹。
如何将以下 Java 行转换为 C++ 代码?
FileInputStream fi = new FileInputStream(f);
byte[] b = new byte[188];
int i = 0;
while ((i = fi.read(b)) > -1)// This is the line that raises my question.
{
// Code Block
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试运行以下代码行,但结果是错误。
ifstream InputStream;
unsigned char *byte = new unsigned char[188];
while(InputStream.get(byte) > -1)
{
// Code Block
}
Run Code Online (Sandbox Code Playgroud) 这个问题解释了如何System.in在运行任务时使用来运行项目中的特定类。
但对我来说,目前它不起作用:尽管我已将application插件和以下几行包含在build.gradle:
mainClassName = "misc.StreamsExp"
run{
standardInput = System.in
}
task stream( type: JavaExec, dependsOn: assemble ){
classpath sourceSets.main.runtimeClasspath
main = "misc.StreamsExp"
}
Run Code Online (Sandbox Code Playgroud)
下面的应用程序代码中的行readLine应该是阻塞的,但事实并非如此:
BufferedReader br = new BufferedReader(new InputStreamReader( System.in ));
String enteredLine = "";
while( enteredLine == null || ! enteredLine.equals( "q" )){
System.out.println( "spuds");
enteredLine = br.readLine();
}
Run Code Online (Sandbox Code Playgroud)
...相反,事情会永远旋转:
土豆土豆
土豆
土豆
...
注意,我使用的是 Windows 10 操作系统,带有 Java 8.91。我尝试过 Windows DOS 控制台和 Cygwin。
NB2 当我在 Eclipse(Gradle STS Eclipse …
我想从给定的分隔符读取std::string命名。默认情况下,当我执行. 如果是,我可以使用.destinationstd::cinstd::istream::operator<<destination << std::cindestinationchar[]std::getline
我们有什么用 std::string?
这是我的文件内容丢失的代码流,我认为可能是 IOUtils.toByteArray() 行有问题,请指导这里实际出了什么问题。
文件内容丢失:
InputStream stream = someClient.downloadApi(fileId);
byte[] bytes = IOUtils.toByteArray(stream);
String mimeType = CommonUtils.fileTypeFromByteArray(bytes);
String fileExtension=FormatToExtensionMapping.getByFormat(mimeType).getExtension();
String filePath = configuration.getDownloadFolder() + "/" ;
String fileName = UUID.randomUUID() + fileExtension;
File file = new File(filePath+fileName);
file.createNewFile();
FileUtils.copyInputStreamToFile(stream,file);
int length = (int)file.length();
Run Code Online (Sandbox Code Playgroud)
现在这里的长度值是0基本上没有内容。让我告诉你,从 downloadApi() 接收到的 inputStream 肯定有内容。但是如果我尝试在代码中进行以下修改,那么我就会得到文件的长度。
文件内容不会丢失:
InputStream stream = someClient.downloadApi(fileId);
byte[] bytes = IOUtils.toByteArray(stream);
String mimeType = CommonUtils.fileTypeFromByteArray(bytes);
String fileExtension=FormatToExtensionMapping.getByFormat(mimeType).getExtension();
String filePath = configuration.getDownloadFolder() + "/" ;
String fileName = UUID.randomUUID() + fileExtension;
stream = new …Run Code Online (Sandbox Code Playgroud) 我正在尝试下载一个简单的.pdf文件,我尝试过日志,但没有错误也没有。我试图调试,但直到昨天它仍然像魅力一样工作,但今天不是,我没有对代码的这一部分进行任何更改。我基于这个问题和答案在 SO。
下面是代码。
有时在 LogCat 它只显示我喜欢这样的消息。
com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251) FileDownloader.downloadFile
在这行代码。
inputStream = urlConnection.getInputStream();
Run Code Online (Sandbox Code Playgroud)
Url 连接的代码。
urlConnection.getResponseCode()
它正在返回 405
MainActivity.java
String newEntry = "http://www.axmag.com/download/pdfurl-guide.pdf";
new DownloadFile().execute(newEntry, "maven.pdf");
private static class DownloadFile extends AsyncTask<String, Void, Void>{
@Override
protected Void doInBackground(String... strings) {
String fileUrl = strings[0]; // -> http://maven.apache.org/maven-1.x/maven.pdf
String fileName = strings[1]; // -> maven.pdf
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File folder = new File(extStorageDirectory, "testthreepdf");
folder.mkdir();
File pdfFile = new File(folder, fileName);
try{
pdfFile.createNewFile();
}catch (IOException e){
e.printStackTrace();
} …Run Code Online (Sandbox Code Playgroud) 我需要将 Reader 对象转换为 InputStream。我现在的解决方案如下。但我担心的是,由于这将处理大量数据,因此会大大增加内存使用量。
private static InputStream getInputStream(final Reader reader) {
char[] buffer = new char[10240];
StringBuilder builder = new StringBuilder();
int charCount;
try {
while ((charCount = reader.read(buffer, 0, buffer.length)) != -1) {
builder.append(buffer, 0, charCount);
}
reader.close();
} catch (final IOException e) {
e.printStackTrace();
}
return new ByteArrayInputStream(builder.toString().getBytes(StandardCharsets.UTF_8));
}
Run Code Online (Sandbox Code Playgroud)
由于我使用 StringBuilder,这会将读取器对象的全部内容保留在内存中。我想避免这种情况。有没有办法可以通过管道传输 Reader 对象?对此的任何帮助表示高度赞赏。