保存文件:
FileOutputStream fo = null;
try {
fo = this.openFileOutput("test.png", Context.MODE_WORLD_READABLE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bitmap.compress(CompressFormat.PNG, 100, fo)
Run Code Online (Sandbox Code Playgroud)
加载文件:
String fname = this.getFilesDir().getAbsolutePath()+"/test.png";
Bitmap bMap = BitmapFactory.decodeFile(fname);
i.setImageBitmap(bMap);
Run Code Online (Sandbox Code Playgroud)
最后一行给出了空指针异常,为什么BitmapFactory.decodeFile返回null?我可以验证文件是否正确保存,因为我可以使用adb将其拉出来并看到png正确显示.
请考虑以下代码:
public static void dumpObjectToYaml(String key, Object O, String path) throws IOException
{
Map<String, Object> data = new HashMap<>();
data.put(key, O);
File F = new File(path);
F.mkdirs();
F.createNewFile();
//write data to File
}
Run Code Online (Sandbox Code Playgroud)
此方法旨在将给定键处的给定Object O写入给定路径的YAML文件中.(如果它不存在则会被创建.)但显然主要部分仍然缺失.
现在按照SnakeYaml的文档,创建一个YAML我只需要创建一个地图并将右边的对象放入对象中,我这样做了.
但无处(至少我没有看到它)被描述如何在某个路径上创建一个yaml文件!
我发现的唯一一件事是:
"Yaml.dump(对象数据)方法接受Java对象并生成YAML文档"
public void testDump()
{
Map<String, Object> data = new HashMap<String, Object>();
data.put("name", "Silenthand Olleander");
data.put("race", "Human");
data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" });
Yaml yaml = new Yaml();
String output = yaml.dump(data);
System.out.println(output);
}
Run Code Online (Sandbox Code Playgroud)
和
"Yaml.dump(对象数据,Writer输出)将生成的YAML文档写入指定的文件/流."
public void testDumpWriter() …Run Code Online (Sandbox Code Playgroud) 我正在将应用程序从Symbian/iPhone移植到Android,其中一部分是将一些数据保存到文件中.我使用FileOutputStream将文件保存到私有文件夹/ data/data/package_name/files中:
FileOutputStream fos = iContext.openFileOutput( IDS_LIST_FILE_NAME, Context.MODE_PRIVATE );
fos.write( data.getBytes() );
fos.close();
Run Code Online (Sandbox Code Playgroud)
现在我正在寻找一种如何加载它们的方法.我正在使用FileInputStream,但它允许我逐字节读取文件,这是非常低效的:
int ch;
StringBuffer fileContent = new StringBuffer("");
FileInputStream fis = iContext.openFileInput( IDS_LIST_FILE_NAME );
while( (ch = fis.read()) != -1)
fileContent.append((char)ch);
String data = new String(fileContent);
Run Code Online (Sandbox Code Playgroud)
所以我的问题是如何使用更好的方式读取文件?
file-io android fileinputstream fileoutputstream android-file
我从互联网上找到了下面的代码,但它不会将打印的控制台写入omt.txt,它只会System.out.println在第二个catch块之后写入语句.如果你运行代码,你就会理解我的意思.我都是想要将控制台上的内容写入"omt.txt"文件,这就是所有...
经过一些回答,我发现我的问题不明确,对不起.我想将控制台输出保存到omt.txt文本文件中.如果在控制台上打印"Hello 123",它也应该在omt.txt文件中.换句话说,打印机上的任何内容都应该同时写在om.txt文件中,或者可以在控制台执行后但是应该是1对1相同!
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class Wrt_file {
public static void main(String[] args) {
System.out.println("THIS is what I see on the console. but not on TEXT file");
File f = new File("omt.txt");
if(!f.exists())
{
try {
f.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
FileOutputStream fos = new FileOutputStream(f);
PrintStream ps = new PrintStream(fos);
System.setOut(ps);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("THIS is what I see on the text …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序保存到用户输入的文件(内部存储)数据中,并在启动时加载此文件并显示内容.我想知道:我在哪里可以找到我的文件(data.txt)?另外,如果我在加载文件时输入"Hello"然后输入"World",我会在同一行看到"HelloWorld",但我希望"Hello"和"World"打印在两个不同的行上.
用于保存文件:
public void writeToFile(String data) {
try {
FileOutputStream fou = openFileOutput("data.txt", MODE_APPEND);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fou);
outputStreamWriter.write(data);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
对于加载文件:
public String readFromFile() {
String ret = "";
try {
InputStream inputStream = openFileInput("data.txt");
if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( …Run Code Online (Sandbox Code Playgroud) 1)目标设定为Android Q与android.permission.WRITE_EXTERNAL_STORAGE
2)使用getExternalStorageDirectory或getExternalStoragePublicDirectory并FileOutputStream(file)保存文件抛出
java.io.FileNotFoundException: /storage/emulated/0/myfolder/mytext.txt open failed: ENOENT (No such file or directory)
3)使用getExternalFilesDirapi并保存成功但即使之后也不会出现MediaScannerConnection.scanFile。
/storage/emulated/0/Android/data/my.com.ui/files/Download/myfolder/mytext.txt
Run Code Online (Sandbox Code Playgroud)
在android Q中将文件从内部存储器复制到SDCARD并刷新的最佳方法是什么。
#include <fstream>
int _tmain(int argc, _TCHAR* argv[])
{
std::ofstream F("con.txt", std::ios::out);
F << "some text in con.txt";
F.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
some text in con.txt
Run Code Online (Sandbox Code Playgroud)
如果我用" con.txt" 替换" ",something.txt那么something.txt将包含字符串" some text in something.txt."
我认为文件con.txt与控制台文件绑定...在第一种情况下真的发生了什么?
我正在尝试将pdf旋转180度,我正在使用ITextSharp库来执行此操作.以下代码取自其网站的示例.但是,我似乎无法找到要导入的正确名称空间以使"FileOutputStream"起作用.
这是一个控制台应用程序,因此不确定Java的"FileOutpuStream"是否可行.
PDFStamper()的结构如下:
PdfStamper(PDF阅读器,Stream os)
public void rotatePDF(string inputFile)
{
// get input document
PdfReader reader = new PdfReader(inputFile);
PdfName pdfName = new PdfName(inputFile);
int n = reader.NumberOfPages;
int rot;
PdfDictionary pageDict;
for (int i = 1; i <= n; i++)
{
rot = reader.GetPageRotation(i);
pageDict = reader.GetPageN(i);
pageDict.Put(PdfName.ROTATE, new PdfNumber(rot + 180));
}
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(inputFile));
stamper.closer();
reader.Close();
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码与iText库正确集成.
import java.io.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
@org.eclipse.jdt.annotation.NonNullByDefault(true)
public class HelloWorld {
public static final String RESULT = "C:\\Users\\administrator\\Pictures\\tuto";
@SuppressWarnings("resource")
public static void main(String[] args) throws DocumentException, IOException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(RESULT));
document.open();
document.add(new Paragraph("Hello World!"));
document.close();
}
}
Run Code Online (Sandbox Code Playgroud)
此代码返回一条错误消息,如下所示.
Exception in thread "main" java.io.FileNotFoundException: C:\Users\valentin.schaefer\Pictures\tuto (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at HelloWorld.main(HelloWorld.java:25)
Run Code Online (Sandbox Code Playgroud)
然而,我是计算机管理员,我通常拥有所有权限帐户.我不明白他为什么要退我Access is denied.
当我尝试将位图存储到存储中时出现此错误 #
File path = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "picture");
if (! path.exists()) {
path.mkdirs();
if (!path.exists()) {
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HH_mm_ss", Locale.CHINA).format(new Date());
File imagePath = new File(path.getPath() + "_" + "IMG_" + timeStamp + ".jpg");
BufferedOutputStream fos;
try {
fos =new BufferedOutputStream(new FileOutputStream(imagePath));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
return imagePath;
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
return null;
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
return null;
}
Run Code Online (Sandbox Code Playgroud)
fos = new BufferedOutputStream(new …
fileoutputstream ×10
android ×5
java ×3
file-io ×2
android-10.0 ×1
android-file ×1
bitmap ×1
c# ×1
c++ ×1
console ×1
file ×1
imageview ×1
itextsharp ×1
output ×1
pdf ×1
printstream ×1
save ×1
snakeyaml ×1
windows ×1