对于文件和目录名称,存在一些受限制的字符(甚至是Windows中的完整文件名).这个问题已经涵盖了他们.
在Java中,有没有办法检索这个禁止字符列表,这取决于系统(有点像检索断路器字符)?或者我可以自己放置列表,检查系统?
编辑:除了一般性问题之外,还有更多关于我特定情况的背景信息.
我使用默认名称,来自某些数据(没有对其内容的真正控制),并且此名称将提供给JFileChooser,作为要使用的默认文件名(with setSelectedFile()).但是,这个会在最后一个无效字符之前截断任何内容.
这些默认名称有时会以"mm/dd/yy"格式结束日期,默认名称只留下"yy",因为"/"是禁止的.因此,检查Exceptions并不是一个真正的选项,因为文件本身甚至还没有创建.
编辑之二:嗯,这让我想到,如果JFileChooser正在截断名称,它可能有权访问这些字符的列表,可以进一步检查这一点.
编辑ter:好的,检查来自JFileChooser的来源显示了一些非常简单的东西.对于文本字段,它使用file.getName().它实际上并不检查无效字符,它只是将"/"作为路径分隔符,并仅保留结尾,即"实际文件名".其他禁止的角色实际上经历了.
文件名包含例如 Legend/Dery//Times
File.WriteAllBytes("/Pictures" + FileName, buffer);
Run Code Online (Sandbox Code Playgroud)
我无法保存文件,因为“/”被视为路径,我也无法删除“/”,因为我需要它进行进一步处理。有没有办法保存这样的文件?
假设我有一个名为a"b.hor的头文件a>b.h,如何转义包含指令中的"or字符?>
// this does not work
#include "a\"b.h"
Run Code Online (Sandbox Code Playgroud) 我试图将我的所有数据写入文本文件,除非我DateTime输入文件名,否则它正在工作.
当前代码如下所示:
string time = DateTime.Now.ToString("d");
string name = "MyName";
File.WriteAllText(time+name+"test.txt","HelloWorld");
Run Code Online (Sandbox Code Playgroud)
我得到这个例外:
mscorlib.dll中发生未处理的"System.IO.DirectoryNotFoundException"类型异常
但据我所知,该File.WriteAllText()方法应该创建一个新文件或覆盖已存在的文件.
有什么建议?
我在Windows 8 bit-64上使用Python v2.x.
问题是,我无法生成一个txt名为实时的文件.
请看我现在的代码:
import sys
import datetime
def write():
# try:
currentTime = str(datetime.datetime.now())
print currentTime #output: 2016-02-16 16:25:02.992000
file = open(("c:\\", currentTime, ".txt"),'a') # Problem happens here
print >>file, "test"
file.close()
Run Code Online (Sandbox Code Playgroud)
我尝试了不同的方法来修改行文件= open(("c:\ ....))但无法创建文本文件,如2016-02-16 16:25:02.992000.txt
有什么建议吗?
自从我们开始为 git 使用子模块以来,我有一些奇怪的事情。我在 Windows 10 上使用 Tortoise git 或 Powershell 或 cmd 或 Linux 命令解释器。
每当我尝试拉一个子模块时,我都会得到这样的信息:
Run Code Online (Sandbox Code Playgroud)PS D:\repositories\git\ProjectAlice\ProjectAliceSkills\PublishedSkills\AliceCore> git pull error: cannot lock ref 'refs/tags/1.0.35>=1.0.0-a4': Unable to create 'D:/repositories/git/ProjectAlice/ProjectAliceSkills/.git/modules/PublishedSkills/skill_AliceCore/refs/tags/1.0.35>=1.0.0-a4.lock': Invalid argument From https://github.com/project-alice-assistant/skill_AliceCore ! [new tag] 1.0.35>=1.0.0-a4 -> 1.0.35>=1.0.0-a4 (unable to update local ref) error: cannot lock ref 'refs/tags/1.0.36>=1.0.0-a5': Unable to create 'D:/repositories/git/ProjectAlice/ProjectAliceSkills/.git/modules/PublishedSkills/skill_AliceCore/refs/tags/1.0.36>=1.0.0-a5.lock': Invalid argument ! [new tag] 1.0.36>=1.0.0-a5 -> 1.0.36>=1.0.0-a5 (unable to update local ref) error: cannot lock ref 'refs/tags/1.0.37>=1.0.0-a5': Unable to create 'D:/repositories/git/ProjectAlice/ProjectAliceSkills/.git/modules/PublishedSkills/skill_AliceCore/refs/tags/1.0.37>=1.0.0-a5.lock': Invalid argument ! …
我的 JSON 看起来像这样:
[
{
"file": "aaa.txt",
"destination": 1
},
{
"file": "bbb.txt",
"destination": 2
},
{
"file": "ccc.txt",
"destination": 3
},
{
"file": "ddd.txt",
"destination": 4
},
{
"file": "eee.txt",
"destination": 9
}
]
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用命令 jq 在 bash 中构建一个脚本来获取 JSON 中的项目数,并在第二个命令中使用文件、目标的值。为了实现我使用的第一个
count=$(curl 'http://mypage/json' | jq length)
Run Code Online (Sandbox Code Playgroud)
我正在获取此元素的数量(计数)(在本例中为 5)。接下来我想构建从 5 到 1的 while 循环,将文件值放入 (nomen omen) 文件中(循环中的脚本应创建名为[destination]的文件,其中[file]作为内容(例如:对于第一个文件,应使用 1 来调用) aaa.txt 作为内容)。而且...这是我的问题 - 如何将我的 JSON 放入数组(或其他内容)中?我尝试使用
arr=$(curl 'http://mypage/json' | jq'.[]')
Run Code Online (Sandbox Code Playgroud)
但它把整个 json 合而为一。你能帮我吗?
我有一个string存储文件名并用于SaveFileDialog.在将文件名分配给FileName属性之前,我需要确保文件名有效(例如,不包含斜杠)SaveFileDialog.我的问题是:在分配文件名之前,是否有快速确保文件名有效的方法?
例:
string fileName = fileNameTextBox.Text;
//Some code here to check validity of fileName
if(fileNameIsValid)
{
saveFileDialog.FileName = fileName;
}
Run Code Online (Sandbox Code Playgroud) 我想创建一个文件,并在文件名中使用项目名称和日期.这是我在Unix机器上使用了一段时间的简单Java代码.
String fileName = projectName + "," + (date.toString());
PrintStream pr = new PrintStream(new File(fileName));
Run Code Online (Sandbox Code Playgroud)
最近,我试图在Windows机器上运行我的代码,我收到此错误:
Exception in thread "main" java.io.FileNotFoundException: Test,Thu Sep 03 12:28:33 EDT 2015 (The filename, directory name, or volume label syntax is incorrect)
Run Code Online (Sandbox Code Playgroud)
当我从文件名中删除日期时,一切都很完美.任何建议文件名中的日期问题是什么以及如何解决它?