Tag: 文件

从Uri类型android创建文件

我正在尝试从库中选择图像然后将此图像转换为文件并通过HttpPost发送它,但我总是得到FileNotFoundException 。 我的代码: 选择照片 public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { if (requestCode == 1) { // currImageURI is the global variable I’m using to hold the content: currImageURI = data.getData(); //Save the currImageUri (URI type) to global variable. photosHolder.getInstance().setOneIm(currImageURI); } } } 转换照片 // First Try File […]

通过java在文本文件中插入数据

我想在文本文件中的某些位置插入数据而不实际覆盖现有数据。我尝试过RandomAccessFile ….但是它也会覆盖它….有没有办法插入数据而不覆盖? -提前致谢

sbt中的工作目录

我希望能够在特定目录中运行java程序。 我认为,参数化工作目录非常方便,因为它可以轻松管理配置。 例如,在一个文件夹中,您可以配置测试,在另一个文件夹中,您可以拥有生产所需的资源。 您可能认为,可以选择操作类路径以包含/排除资源,但只有当您对存储在类路径中的资源感兴趣并使用Classloader.getResource(r)引用它们时,此解决方案才有效。 但是如果你有一些外部配置并且想要使用File file = new File(“app.properties”);类的简单指令来访问它会File file = new File(“app.properties”); ? 让我们看看普通的例子。 您的应用程序使用app.properties文件,您可以在其中存储外部服务的凭据。 应用程序在工作目录中查找此文件,因为您使用了提到的File file = new File(“app.properties”); 访问它的说明。 在测试中,您希望使用特定于测试的app.properties 。 在集成测试中,您希望使用特定于其他环境的app.properties 。 最后,当您构建和发布应用程序时,您希望提供其他app.properties文件。 通过键入File file = new File(“app.properties”);您想要访问的所有这些资源总是以相同的方式访问File file = new File(“app.properties”); 而不是(伪代码): if(configTest) file = File(“testWorkDir/app.properties”); else if(config2) file = File(“config2WorkDir/app.properties”); else file = File(“app.properties”); 或者不使用classpath中的资源 this.getClass.getClassLoader.getResource(“app.properties”); 当然你是聪明的程序员,你使用构建工具,如maven,gradle或sbt 🙂 从一开始就应该这样做。 […]

从Java获得毫秒分辨率的文件mtime

当我使用Files.getLastModifiedTime从Java读取文件的mtime时,返回值被截断为整秒。 我知道这可以在其他系统上运行,以获得毫秒级的分辨率,那么我的可能有什么不同呢? 这是一个完整的独立测试,可以编译和运行: import java.nio.file.attribute.FileTime; import java.nio.file.Files; import java.nio.file.Paths; public class Test { public static void main(String[] args) throws java.io.IOException { FileTime timestamp = Files.getLastModifiedTime(Paths.get(“/tmp/test”)); System.out.println(timestamp.toMillis()); } } 输出是(使用我的特定测试文件) 1405602038000 ,而ls显示: $ ls –full-time /tmp/test -rw-rw-r– 1 daniel daniel 0 2014-07-17 16:00:38.413008992 +0300 /tmp/test 我希望Java输出为1405602038413 。 我用ext4在Linux上运行。 我尝试了openjdk 1.7和Oracle jdk 1.8。

将文件从OS拖放到JTable java中

有人能告诉我我做错了什么吗? 我能够使用常规面板进行拖放操作,但现在尝试使用表格,我无法对其进行排序。 我对Points和DropTargets感到困惑。 不要介意“添加”按钮。 我觉得我需要先处理DnD。 public class Table extends JFrame implements ActionListener { private JTable table; private JScrollPane scroll; private JButton add; private JFileChooser choose; private JMenuBar menubar; private JMenu menu; private JMenuItem file; private DefaultTableModel tm = new DefaultTableModel(new String[] { “File”, “File Type”, “Size”, “Status” }, 2); public Table() { // String column […]

无法从java中的URL下载文件

我正在制作一个从URL下载文件的程序。 下载始终开始,但尚未完成。 例如,如果文件的大小为3 MB,程序下载只有一半,所以我无法打开下载的文件。 但程序说该文件已成功下载。 public class FileDownloader { public static void main (String [] args) throws IOException { InputStream fileIn; FileOutputStream fileOut; Scanner s = new Scanner(System.in); System.out.println(“Enter URL: “); String urlStr = s.nextLine(); URL url = new URL(urlStr); URLConnection urlConnect = url.openConnection(); fileIn = urlConnect.getInputStream(); System.out.println(“Enter file name: “); String fileStr = s.nextLine(); […]

流式传输时缓冲区字节数组如何连续填充?

下面的代码用于读取Files int bytesRead; byte[] bytes = new byte[1000]; //buffer FileInputStream fis = new FileInputStream(uploadedFile); while ((bytesRead = fis.read(bytes)) != -1) { fis.read(bytes, 0, bytesRead); } fis.close(); 根据read()方法的api 将此输入流中的b.length个字节数据读入一个字节数组。 此方法将阻塞,直到某些输入可用。 没有指定它重新填充bytes数组,但是流填充array直到file成功read. 。 但内部如何保持这种魔力? 我看过源代码或读取方法 public int More …read(byte b[]) throws IOException { 214 return readBytes(b, 0, b.length); 215 } 和readBytes的源代码是 200 private native int More …readBytes […]

将自定义属性或元数据添加到文件java

我的文件需要一个名为“加密使用”的额外属性。 但这给出了“IllegalArgumentExeption”。 我知道它为什么会出现这个错误,“使用加密”并不是一个属性,但有没有办法可以强制它呢? 或者将自定义元数据添加到文件中? Path path = new File(“/propertyfiles/encdec.properties”).toPath(); try{ Files.setAttribute(path, “encryption used”, “testtesttest”); }catch(IOException e){ System.out.println(e.getMessage()); } try{ System.out.println(Files.getAttribute(path, “encryption used”)); }catch(IOException e){ System.out.println(e.getMessage()); }

Android – 从url下载JSON文件

是否可以从URL下载包含JSON数据的文件? 此外,我需要获取的文件没有文件扩展名,这是一个问题还是我可以强制它下载时具有.txt扩展名? 更新:我忘了提到,网站需要输入用户名和密码才能访问我所知道的网站。 有没有办法在检索文件时输入这些值?

如何在jsp中获取上传文件的完整路径?

在我的jsp页面中,使用文件上传并将字符串文件传递给java页面以复制到特定文件夹。 我想要复制文件的完整路径。 但我只得到一个带扩展名的文件名。 扫描文件:ABC.pdf 它只显示:ABC.pdf 我想表明:c:/abc.pdf