我有一个程序,我需要在*nix和windows下运行.因为程序从文件中获取文件路径,所以问题是如何处理\vs /问题.
我目前的想法是放入一个正则表达式,根据我所使用的系统将错误的一个转换为正确的正则表达式.这将使任一类型在任一系统上都起作用.除了现在我有两个问题,有没有人看到任何其他问题?
(其他更好的解决方案非常受欢迎)
编辑:主要问题是让Windows路径在unix上运行而不是相反.
更新:
我可以把这个问题解决成一个更简单的问题:
我想弄清楚正确的正则表达式是用两个反斜杠替换任何一次反斜杠的情况.
我想转此:
vlc.plugin.path = C:\Program Files\JekyllV0.9.2\\VLC_1.0.0\\plugins
Run Code Online (Sandbox Code Playgroud)
成:
vlc.plugin.path = C:\\Program Files\\JekyllV0.9.2\\VLC_1.0.0\\plugins
Run Code Online (Sandbox Code Playgroud)
原始问题:
我想使用Perl Regex在文件中更改以下内容:
我尝试了以下方法:
perl" -p -i.orig -e "s#\\#\\\\#g" -e "s#/#\\\\#g" %VIDEOLOG_PROPERTIES_FILE%
Run Code Online (Sandbox Code Playgroud)
%VIDEOLOG_PROPERTIES_FILE%包含的位置:
vlc.plugin.path = C:\Program Files\JekyllV0.9.2/VLC_1.0.0/plugins
Run Code Online (Sandbox Code Playgroud) 我已将用户的相应图片保存在名为"profileportraits"的文件夹中.因此,当用户上传他们的照片时,它会保存到该文件夹中.此外,我还将"纵向路径",即照片的位置/名称保存到MySQL数据库中以获取用户数据.我的问题是:我能够回显纵向路径,用户的肖像存储在页面上.但是如何将实际照片显示在页面上?即如何将文件路径转换为实际图片?我是否必须将从MySQL检索到的路径与目录相匹配?或者是其他东西?下面是一些代码.
{
echo $row['PortraitPath'];
}
Run Code Online (Sandbox Code Playgroud)
通过上面的代码,我能够回显实际路径,即:profileportraits/DSC00310.JPG.我只是想将图片的这条路径转换为实际图片.谢谢.
我是android的新手,不太了解它.我已将图像导入到我的项目中,当我尝试使用文件检查文件是否存在时.
这是我用过的代码..
existingFileName="res/drawable-ldpi/login.png";
File f= new File(existingFileName);
// Log.d("Image path",);
if(f.exists())
{
Log.d("EXISTS", "====File Exists===");
}
Run Code Online (Sandbox Code Playgroud)
它仍然显示我没有图像存在.
aby,谢谢你的帮助
我试图确定给定的路径是指向文件还是目录.目前我的逻辑很简单,涉及以下检查
if (sourcePath.Contains(".")) // then treat this path as a file
Run Code Online (Sandbox Code Playgroud)
上面的问题是文件夹名称也可以包含句点.我希望能够确定给定的路径是文件的路径,而不必尝试实例化文件流类型并尝试访问它或类似的东西.
有没有办法做到这一点?
提前致谢
在Perl中,如果给定文件路径,您如何找到第一个现有的祖先?
例如:
/opt/var/DOES/NOT/EXIST/wtv/blarg.txt和目录/opt/var/DOES/不存在但目录/opt/var/是,则结果应为/opt/var/./home/leguri/images/nsfw/lena-full.jpg和目录/home/leguri/images/nsfw/不存在,但目录/home/leguri/images/不存在,结果应该是/home/leguri/images/.是否存在执行此操作的模块或功能,或者只是将路径分开/并测试存在?
我正在尝试使用下面的代码将文件从硬盘加载到文件输入流.
package com.filefinder1;
import java.io.File;
import java.io.FileInputStream;
import android.app.Activity;
import android.os.Bundle;
public class FileFinder_1Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try
{
String pathToFile = "C:\\\\Koala_Small.jpg";
System.out.println("File Path: "+pathToFile);
File file = new File(pathToFile);
FileInputStream fileInputStream = new FileInputStream(file);
}
catch (Exception ex)
{
System.out.println("Error Catch Triggered: "+ex);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在文件路径上尝试了一些不同的变体,但没有任何东西都返回以下错误:
01-10 10:59:06.189: I/System.out(2218): Error Catch Triggered: java.io.FileNotFoundException: /C:\\Koala_Small.jpg (No such file or directory)
Run Code Online (Sandbox Code Playgroud)
一些额外的"/"似乎被添加到文件路径(不知道为什么).当我要求系统在尝试将其加载到文件变量之前打印文件路径时,它返回: …
我想问一下,如果可以保留用户的上传路径,他从他的计算机中选择哪个文件将被上传,如路径"C:\ Users\User1\Desktop\1.jpg",我想学习的原因关于这是在用PHP提交表格后,当他未通过验证时,我希望他不要重新选择它.
摘要:如果用户在其他字段的验证失败,如何保存路径:用户表单中的C:\ Users\User1\Desktop\1.jpg
谢谢
我是一名java初学者.我编写了一个简单的程序,使用JFileChooser中的showSaveDialoge()将一些内容写入文件.代码包括以下内容.
public static void main(String arg[]) throws IOException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
{
JFrame frame = new JFrame();
JFileChooser fc = new JFileChooser();
try {
File file = new File("fileName.txt");
fc.setSelectedFile(file);
int r = fc.showSaveDialog(frame);
if(r == JFileChooser.APPROVE_OPTION)
{
FileWriter writer = new FileWriter(file);
writer.append("Data inside the file");
writer.flush();
writer.close();
}
else if(r == JFileChooser.CANCEL_OPTION) {
System.out.println("Do nothing for CANCEL");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "File could not be written, try again.");
}
}
Run Code Online (Sandbox Code Playgroud)
代码被执行并且保存对话框已经到来.但是当我点击对话框上的SAVE按钮时,什么也没发生.该文件尚未保存在所选位置.可能是什么原因.?提前致谢.
我在应用程序启动时从Internet下载文件,然后在本地保存并使用其数据.我希望下载的文件在开始时每次都覆盖以前的文件,但我无法获取覆盖的数据,它会继续显示以前的文件.如果我通过检查它是否存在来删除它,那么它会给出一个错误"无法复制到"文档",因为具有相同名称的项目已经存在."".如果不创建新文件的话检查它的存在然后它给出了这个错误:
"从文件读取时发生错误.错误描述:%@无法打开文件"Splashk.text",因为没有这样的文件."
这是我的代码:
// checking file existence
do{
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = URL(fileURLWithPath: path)
let filePath = url.appendingPathComponent("Splashk.text").path
let fileManager1 = FileManager.default
if fileManager1.fileExists(atPath: filePath) { // if available, delete the file and re create an empty file
print("FILE AVAILABLE")
try fileManager1.removeItem(atPath: filePath)
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let path = dir.appendingPathComponent("Splashk.text")
// writing
do {
try text.write(to: path, atomically: true, encoding: String.Encoding.utf8)
}
catch {/* error handling here …Run Code Online (Sandbox Code Playgroud) filepath ×10
file ×4
android ×2
file-upload ×2
java ×2
perl ×2
php ×2
windows ×2
backslash ×1
c# ×1
directory ×1
file-exists ×1
file-io ×1
forms ×1
html ×1
ios ×1
jfilechooser ×1
mysql ×1
portability ×1
swift ×1
swift3 ×1
swing ×1
unix ×1
validation ×1