import java.util.*;
import java.io.*;
public final class FileListing2
{
public static void main(String... aArgs) throws FileNotFoundException
{
File startingDirectory= new File(aArgs[0]);
List<File> files = FileListing.getFileListing(startingDirectory);
//print out all file names, in the the order of File.compareTo()
for(File file : files )
{
System.out.println(file);
}
}
static public List<File> getFileListing(File aStartingDir) throws FileNotFoundException
{
validateDirectory(aStartingDir);
List<File> result = getFileListingNoSort(aStartingDir);
Collections.sort(result);
return result;
}
static private List<File> getFileListingNoSort(File aStartingDir) throws FileNotFoundException
{
List<File> result = new ArrayList<File>();
File[] filesAndDirs = aStartingDir.listFiles();
List<File> …Run Code Online (Sandbox Code Playgroud) 我一直在努力学习我的第一个正则表达式.在编译期间,Pattern并Matcher不断收到cannot find symbol错误.
我只是改变import java.util.*了import java.util.regex.*,它就像一个梦想.
我是印象中import荷兰国际集团java.util.*将也带来java.util.*.*等等那是不是这样的?我找不到任何解决这个具体问题的文件....
我正在运行此PowerShell命令:
Get-ChildItem .\tx\*.htm | Rename-Item -NewName {$_.Name -replace '\.htm','.tmp'}
Run Code Online (Sandbox Code Playgroud)
当文件名包含方括号 - [和/或]- 时,会收到以下错误,这是可以理解的,因为这些在PowerShell语法中有意义.
Rename-Item : Cannot rename because item at
'Microsoft.PowerShell.Core\FileSystem::C:\users\xxxxx\desktop\tx\
Foofoofoofoo_foo_foo_[BAR]_Foofoofoofoo_foofoofoo.htm' does not exist.
At C:\users\xxxxx\desktop\foo002.ps1:59 char:39
+ Get-ChildItem .\tx\*.htm | Rename-Item <<<< -NewName { $_.Name -replace '\.htm','.tmp' }
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
路径中的所有其他文件具有相似的名称(Phrase_With_Underscores.htm),并且无需重大事件即可重命名.任何人都有这方面的经验,知道如何反击它,以便我可以操纵这些文件?
我在 PowerShell 脚本中进行了许多字符串替换。
foreach ($file in $foo) {
$outfile = $outputpath + $file
$content = Get-Content ($file.Fullname) -replace 'foo','bar'
Set-Content -path $outfile -Force -Value $content
}
Run Code Online (Sandbox Code Playgroud)
我已经验证(通过$outfileand 的控制台日志记录$content,我没有在上面的代码中显示)正在选择正确的文件,正在-replace准确地更新内容,并且$outfile正在创建 s。但是,每个输出文件都是一个 0 字节的文件。该Set-Content行似乎没有将数据写入文件。我试过管道Set-Content到Out-File,但这只会给我一个错误。
当我更换Set-Content使用Out-File,我得到一个运行时错误Out-File : A parameter cannot be found that matches parameter name 'path'.,即使我能输出$outfile到控制台,并认为它是一个有效的路径。
是否有额外的步骤(例如 close-File 或 save-file 命令)我需要采取或不同的顺序,我需要通过管道将某些东西$content写入我的$outfile? 我缺少什么组件?
我正在设计一个需要"位置"字段的应用程序.该位置的值是"3241","4112","ND"和"TRAVEL",我正在尝试设置enum包含这些值的值.
我开始了
enum projectLocation {3241,4112,ND,TRAVEL};
Run Code Online (Sandbox Code Playgroud)
但是值3241和4112表示语法错误identifier expected- 对于第一个值enum.如果我理解enum正确,那是因为上面的语句正在寻找和enum的整数indeces 的值.这是正确的假设吗?32414112
我尝试用以下内容覆盖它
enum projectLocation {3241=0,4112,ND,TRAVEL};
Run Code Online (Sandbox Code Playgroud)
和
enum projectLocation {3241=0,4112=1,ND=2,TRAVEL=3};
Run Code Online (Sandbox Code Playgroud)
但我仍然在3241值上得到相同的语法错误.但有趣的是,对这两个报表,对4112 NO语法错误,但我得到can't find the namespace or name ND和...TRAVEL
有意义的是,enum不允许混合使用字符串和整数,而且我还有另外两个enum可以正常运行的s,它们只是字符串值的列表,证实了这个理论.有没有办法强制enum接受数值作为字符串?我无法在MSDNs C#文档中找到任何引用.
我有一个脚本可以将来自不同源的大量文件复制到一个目录进行备份.错误输出的脚本的唯一步骤在路径和文件名中都有空格:\\server\Network Shares\Transfer\tu3\tu3 Code.mdb
我得到错误copy-item : Cannot find path '\\server\Network Shares\Transfer\tu3\tu3 Code.mdb' because it does not exist.,我假设它是因为路径或文件名中的空格.PowerShell是否允许在完全限定的路径中使用空格?如果没有,我怎么能得到这个文件?
这是相关代码(My $Dest被定义为脚本的全局变量):
$TU3CodeUpdatedPathname = "\\server\Network Shares\Transfer\tu3\"
$TU3CodeUpdatedFilename = "tu3 Code.mdb"
$TU3CodeUpdated = $TU3CodeUpdatedPathname + $TU3CodeUpdatedFilename
#
$Source = $TU3CodeUpdated
$Dest = $VMShareSpacePathname
#
copy-item $Source $Dest
Run Code Online (Sandbox Code Playgroud) 客户端有一个需要擦除某些名称的Excel文件.特别是,我们试图在名称上隔离后缀(例如,Jr.,Sr.,III等).名称始终LastName, FirstName, Suffix在单元格中格式化,我试图计算单元格中逗号的数量.如果单元格中有多个逗号,我可以将该单元格标识为具有名称后缀.
但是,COUNTExcel中的所有函数都计算CELLS的实例,而不是单元格中的字符.是否有一个函数可以计算单元格中特定字符的出现次数并返回该计数?
对于家庭作业,我正在使用以下内容.这是一个指定的类结构,我知道这不是最好的设计.
Class | Extends | Variables
--------------------------------------------------------
Person | None | firstName, lastName, streetAddress, zipCode, phone
CollegeEmployee | Person | ssn, salary,deptName
Faculty | CollegeEmployee | tenure(boolean)
Student | person | GPA,major
Run Code Online (Sandbox Code Playgroud)
所以在教师班......
public class Faculty extends CollegeEmployee
{
protected String booleanFlag;
protected boolean tenured;
public Faculty(String firstName, String lastName, String streetAddress,
String zipCode, String phoneNumber,String ssn,
String department,double salary)
{
super(firstName,lastName,streetAddress,zipCode,phoneNumber,
ssn,department,salary);
String booleanFlag = JOptionPane.showInputDialog
(null, "Tenured (Y/N)?");
if(booleanFlag.equals("Y"))
tenured = true;
else
tenured = false;
}
} …Run Code Online (Sandbox Code Playgroud) 尽管Java教程,维基百科搜索,stackoverflow拖钓以及阅读代码示例的时间,构造函数仍然混淆了我的废话.我有三个相关的问题,我一直试图回答,以帮助我更好地理解构造函数.
首先,我一直认为构造函数需要与它们的类相同.考虑:
public class Money {
public Money(long l) {
this.value = l;
}
public Money(String s) {
this.value = toLong(s);
}
public long getLong() {
return this.value;
}
public String getString() {
return toString(this.value);
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这是四个构造函数......对吗?因此,它似乎是构造不是命名一样包含它们允许类.有人可以证实吗?
其次,我似乎有一个阻止理解集合和获取方法的块.考虑:
public class GetSetSample {
public int getFoo() {
return int Foo;
}
public void setFoo(int fooValue) {
int Foo = fooValue;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我不能这样做:
public class getFoo(int fooValue){
foo=fooValue;
}
Run Code Online (Sandbox Code Playgroud)
并使用foo = getFoo(12)其他一些类/方法?
第三个问题有点深奥,但会帮助我设想更大的图景......这是我的学习风格,有利于我在调试时跟踪程序流的能力.这些get …
作为番茄钟技术的粉丝,我正在制作一个倒数计时器,让我完成我的作业任务.然而,这个特殊的项目不是功课.:)
Stack有很多关于在用户输入之前使用定时器来控制延迟的问题,但在独立的定时器上却没有太多问题.我从朋友那里遇到了这段代码,并且已经学习了Java Documentation的课程.
public class Stopwatch {
static int interval;
static Timer timer;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Input seconds => : ");
String secs = sc.nextLine();
int delay = 1000;
int period = 1000;
timer = new Timer();
interval = Integer.parseInt( secs );
System.out.println(secs);
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
System.out.println(setInterval());
}
}, delay, period);
}
private static final int setInterval()
{
if( interval== 1) timer.cancel();
return --interval;
}
} …Run Code Online (Sandbox Code Playgroud) java ×5
powershell ×3
c# ×1
constructor ×1
enums ×1
excel ×1
file-io ×1
file-rename ×1
function ×1
import ×1
inheritance ×1
oop ×1
package ×1
syntax ×1