我有代码要求用户输入日志文件名,以便它知道要解析和排序的文件.
为了能够正确解析,FileSource.parseXML读取变量类型File.因此,输入String需要转换为File.怎么做?
import java.io.*;
import java.util.*;
public class Main
{
public static void main( String[] args )
{
FileSource src;
File fd=null;
int rc = 0;
int count = 0;
Record rec;
//ask user what file to parse
Scanner input = new Scanner(System.in);
System.out.println("Enter file name:");
String filename = input.nextLine();
//TODO turn filename into fd
//parse the records
src = FileSource.parseXML( fd );
//print out the number of records parsed
rc = …Run Code Online (Sandbox Code Playgroud) 如何在 Windows 批处理脚本中的 for 循环中迭代令牌?
我正在编写一个脚本,允许用户搜索文件并打印出该文件的父目录。到目前为止,我可以从文件名获取完整路径,但我只需要父目录。工作正常,但需要一种更有效的方式来迭代令牌。
这是我所拥有的内容的片段。file 变量是要搜索的文件,Path 变量是文件的完整路径。
fOR /F "tokens=1-25 delims=\" %%i IN ("!thePath!") DO (
if %%j equ %file% set theParent= %%i
if %%k equ %file% set theParent= %%j
if %%l equ %file% set theParent= %%k
if %%m equ %file% set theParent= %%l
if %%n equ %file% set theParent= %%m
if %%o equ %file% set theParent= %%n
if %%p equ %file% set theParent= %%o
if %%q equ %file% set theParent= %%p
if %%r equ %file% set theParent= …Run Code Online (Sandbox Code Playgroud)