Tag: 文件

android写给sdcard

我无法写入SDCARD。 我无法创建目录或文件,我错过了我在清单中有这个 这在我的代码中 btnSave.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { // Create a directory; all ancestor directories must exist boolean success = (new File(“/sdcard/Methods/”)).mkdirs(); //create file try{ File myFile = new File(“/sdcard/Methods/Method 1 Square.xml”); File file = myFile; // Create file if it does not exist boolean success1 = file.createNewFile();} catch (IOException e) {} […]

在我的ftp服务器上使用JAVA上传后清空文件

尝试使用java在我的ftp服务器上上传文件时,我遇到了一些问题。 这是我的代码function: public static void upload_files(String un, String pw, String ip, String f){ try { FTPClient client = new FTPClient(); client.connect(ip); client.login(un,pw); InputStream is = new FileInputStream(f); client.storeFile(“test2”,is); is.close(); } catch(Exception e) { e.printStackTrace(); } } “f”是我要上传的文件的路径(即“C:\ myfile”)。 编译期间没有错误,一个文件很好地发送到服务器,但遗憾地是空的(0字节)。 我还注意到,即使我认为我有一个非常好的ISP,也需要很长时间才能简单地上传一些文本(大约40秒)。 我提前感谢所有帮助我的人。 问候,

如何使用JFileChooser保存txt文件?

鉴于此方法: public void OutputWrite (BigInteger[] EncryptCodes) throws FileNotFoundException{ JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.showSaveDialog(null); String path = chooser.getSelectedFile().getAbsolutePath(); PrintWriter file = new PrintWriter(new File(path+”EncryptedMessage.txt”)); for (int i = 0; i <EncryptCodes.length; i++) { file.write(EncryptCodes[i]+ " \r\n"); } file.close(); } 忽略变量名称,此方法的作用是在名为EncryptedMessage.txt的项目文件夹内生成的txt文件中写入EncryptCodes数据。 我需要的是一种保存该txt文件而不是项目文件夹的方法,以便在运行期间保存在用户指定的位置(打开另存为对话框)。 我认为它可以由JFilechooser完成,但我无法让它工作。

将文件从目录复制到另一个目录,具有文件和目录的路径

在我的Android应用程序中,我想将文件从一个目录复制到另一个目录,我有文件filePath的路径, dirPath在必须复制文件的内容中有目录dirPath的路径。 我尝试了很多方法,但没有任何帮助,有些方法只能制作一些空的(0 kb)文件,其名称与我的文件名不同。 请帮助请:) 这是代码的一部分,如果它对你有用,我有两个按钮用于Gallery和Camera,我必须从那里选择图像 Button btnCam = (Button) dialog.findViewById(R.id.btncamera); btnCam.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.cancel(); Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, 2500); } }); //end of camera button Button btnGal = (Button) dialog.findViewById(R.id.btngalary); btnGal.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.cancel(); Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); […]

读取Jar中的文件(ZipInputStream等)

我正在用java编写一个游戏,并且所有工作都或多或少都正常,正如您在编码时所期望的那样,但我遇到的一个问题是将图像和声音文件读入游戏。 我编写了一个在Eclipse中运行良好的方法,但是一旦我尝试在可运行的jar中测试它(使用Eclipse导出),我的游戏就会卡在加载屏幕上,因为它无法读取文件。 我意识到你不能在Jar中创建File对象的问题,你必须使用流。 这可能有效,但图像/声音位于文件夹中,我不太确定如何使用流读取文件夹中的文件(即不知道文件的数量)。 因此我尝试使用ZipInputStream重写该方法(我在发布此解决方案之前找到了解决方案时的建议)。 这就是现在的方法: imageMap = new HashMap(); String imagebase = “/images”; CodeSource src = PlatformerCanvas.class.getProtectionDomain().getCodeSource(); if(src != null) { URL jar = src.getLocation(); try { ZipArchiveInputStream zip = new ZipArchiveInputStream(jar.openStream()); ZipArchiveEntry ze = null; while((ze = zip.getNextZipEntry()) != null) { System.out.println(ze.getName()); if(ze.getName().startsWith(imagebase) && ze.getName().endsWith(“.png”)) { loading++; imageMap.put(ze.getName().replaceAll(imagebase + “/”, “”), ImageIO.read(this.getClass().getResource(ze.getName()))); } […]

eclipse中的java文件相对路径

三天我试图弄清楚如何使用相对文件路径读取文件。 在eclipse中,这个编译并且工作得很好,但是当我导出app时。 它说它找不到文件。 这是我工作的截图和代码。 这段代码有效,但只有在eclipse中,它才能编译并完美地完成工作。 但是当我将其导出为runnable jar文件时,我得到一个错误,它无法找到licenca.txt BufferedReader in = new BufferedReader(new FileReader(new File(“licenca.txt”).getPath())); String str; while ((str = in.readLine()) != null) { taLicenca.append(str + “\n”); } 这是我的项目文件的屏幕截图 文件http://img207.imageshack.us/img207/5317/63432445.png 我试过使用扫描仪function,仍然是相同的结果,它在eclipse中工作,但在导出时不起作用。 这是错误消息: 错误http://img98.imageshack.us/img98/1771/greskaa.png

从内部存储获取映像时出现FileNotFoundException

档案==> ==> 正如我在这个图片流中所展示的那样,我的图像(960-Minimalism.jpg)位于内部手机存储内部,我正试图通过File类获取它的进一步使用路径。 我还试过了什么 第一名: String filePath = MainActivity.this.getFilesDir().getAbsolutePath(); File file = new File(filePath, “960-Minimalism.jpg”); 第二: File file = new File(Environment.getExternalStorageDirectory()+ “/Android/data/”+ getApplicationContext().getPackageName() + “/Files/”+”960-Minimalism.jpg”); 极端硬编码方式: File file = new File(“/data/data/com.example.net/files/”,”960-Minimalism.jpg”) 这里的例外情况与第1期相同 我知道我在这里遗失了一些东西或做错了什么但是到现在为止还没找到。 任何的想法.. 编辑 请不要认为我有三个问题。 以上是我尝试过的解决方案,但没有奏效。 但问题是我想要获取内部存储器中的图像路径。 这就是我在这里删除我的例外的原因。

如何将多个对象写入文件?

我想将多个对象写入文件,但问题是我没有一次写入所有对象。 我必须写一个对象然后关闭文件,然后可能在一段时间之后我想将另一个对象添加到同一个文件中。 我目前正在将其作为FileOutputStream(“filename”, true)因此它会将对象附加到文件末尾而不会覆盖它。 但我得到这个错误: java.io.StreamCorruptedException:无效的类型代码:AC 任何想法如何解决这个问题? 谢谢,

如何获取文件属性而不下载

如果我有文件的URL,如何在不下载文件的情况下获取文件属性(例如日期)。 我必须实现更新检查器。 为此,我将比较两个文件的日期。 一个是本地的,第二个是相同的文件,但在服务器上有最新的日期。 我知道第二个文件的URL。 请给我一个建议 – 如何在不下载的情况下查看第二个文件的日期? 我想过通过hash进行比较,但我需要检查服务器上的文件是否是最新版本?

通过从MediaStore.ACTION_IMAGE_CAPTURE中选择图像来创建文件

我允许用户从他们的照片库中选择一张图片: Intent pickIntent = new Intent(); pickIntent.setType(“image/*”); pickIntent.setAction(Intent.ACTION_GET_CONTENT); Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); String pickTitle = “Select or take a new Picture”; // Or get from strings.xml Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle); chooserIntent.putExtra ( Intent.EXTRA_INITIAL_INTENTS, new Intent[] { takePhotoIntent } ); startActivityForResult(chooserIntent, SELECT_PICTURE); 在onActivityResult我想从所选图像中创建一个新File 。 这是我在做的事情: @Override public void onActivityResult(int requestCode, int resultCode, Intent data) […]